{"id":8361,"date":"2022-09-13T04:58:01","date_gmt":"2022-09-12T23:28:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/"},"modified":"2022-09-13T04:58:01","modified_gmt":"2022-09-12T23:28:01","slug":"solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/","title":{"rendered":"[Solved] Why is the comma in two text fields put at the same time without focusing?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-63358035\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"63358035\" data-parentid=\"63356963\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>Why is the comma placed without focusing text fields?<\/p>\n<\/blockquote>\n<p>Because you are using the global <code>keyPressed()<\/code> event.<br \/>\nThis condition: <code>if (e.getKey() == ',')<\/code> checks that the <code>,<\/code> key is pressed and it&#8217;s redundant to check twice in your case. It&#8217;s equivalent to this simpler\/cleaner snippet:<\/p>\n<pre><code>public void keyPressed(KeyEvent e) {\n  if (key == ','){\n    X9.setText(X9.getText() + ',');\n    X10.setText(X10.getText() + ',');\n  }\n}\n<\/code><\/pre>\n<p>There isn&#8217;t any check if a field is focused or not.<\/p>\n<p>You probably meant something like this ?:<\/p>\n<pre><code>public void keyPressed(KeyEvent e) {\n  if (key == ',') {\n    if (X9.isFocus()) {\n      X9.setText(X9.getText() + ',');\n    }\n    if (X10.isFocus()) {\n      X10.setText(X10.getText() + ',');\n    }\n  }\n}\n<\/code><\/pre>\n<p>Overall it&#8217;s unclear what the main goal is. Perhaps there&#8217;s a simpler way to achieve it ?<\/p>\n<p>Remeber that you can listen for individual controlP5 component events via <code>void controlEvent(ControlEvent event)<\/code>. See <strong>Processing &gt; Examples &gt; Contributed Libraries &gt; ControlP5 &gt; controllers &gt; ControlP5Textfield<\/strong> for a nice demo.<\/p>\n<p>Additionally I recommend formatting\/keeping code tidy. It may be a quick throw-away sketch, but buiilding a good habbit will pay off as you&#8217;ll spend way more time reading code than writing code. As your programs become larger and larger you&#8217;ll want to make life easier for your future self \ud83d\ude42<\/p>\n<p><strong>Update<\/strong> Based on your comments here is a minimal sketch that should allow you control a floating point number between 7.4 and 16.8 and an integer number between 1800-1900:<\/p>\n<pre><code>import controlP5.*;\n\nControlP5 cp5;\n\nint serialInt = 1800;\n\nfloat serialFloat = 7.4;\n\nvoid setup() {\n  size(300,300);\n  noStroke();\n  cp5 = new ControlP5(this);\n  \n  cp5.addNumberbox(\"serialInt\")           \/\/ notice the component name matches the variable name: controlP5 links the two for you\n     .setBroadcast(false)                 \/\/ disable events while we update value specific properties \n     .setPosition(100,100)                \/\/ screen location\n     .setSize(100,20)                     \/\/ screen dimensions\n     .setMultiplier(10)                   \/\/ set the sensitifity of the numberbox: each step is 10\n     .setDirection(Controller.HORIZONTAL) \/\/ change the control direction to left\/right\n     .setRange(1800, 9000)                \/\/ set minimum, maximum value\n     .setBroadcast(true)                  \/\/ enable events (after setting range)\n     ;\n  \n\n  cp5.addNumberbox(\"serialFloat\")\n     .setBroadcast(false)                 \/\/ disable events while we update value specific properties\n     .setPosition(100,140)                \/\/ screen location\n     .setSize(100,20)                     \/\/ screen dimensions\n     .setMultiplier(0.01)                 \/\/ set the sensitifity of the numberbox: each step is 0.01\n     .setDirection(Controller.HORIZONTAL) \/\/ change the control direction to left\/right\n     .setRange(7.4,16.8)                  \/\/ set minimum, maximum value\n     .setBroadcast(true)                  \/\/ enable events (after setting range)\n     ;\n  \n}\nvoid draw() {\n  background(0);\n}\n\/\/ gets called whenever a component updates value\nvoid controlEvent(ControlEvent event){\n  println(event.getController().getName(),\"changed value to\",event.getValue(),\"serialInt = \",serialInt,\"serialFloat = \",serialFloat);\n}\n<\/code><\/pre>\n<p>Click and drag horizontally to change values.<br \/>\nNotice a few helpful things that ControlP5 provides:<\/p>\n<ul>\n<li>if you name your controller the same name as the controller the two are connected automatically: huge time saver. (If you can&#8217;t use variable safe name you can look at <strong>Examples &gt; Contributed Examples &gt; ControlP5 &gt; use &gt; ControlP5plugTo<\/strong>)<\/li>\n<li>you can easily set the range and precision desired<\/li>\n<li>you can use <code>controlEvent()<\/code> optionally to tell when a value changed<\/li>\n<\/ul>\n<p>Regarding data over serial:<\/p>\n<ul>\n<li>7.4 to 16.8 range is easy if you only need .1 precision: simply multiply the value by 10 bringing it to 74 to 168 which fits within a single byte (0-255 range)<\/li>\n<li>1800 to 9000 range is trickier because 9000-1800 = 7200 steps in between. For this precision you would need at least 13 bits (2 ^ 13 = 8192 so can fit 7200 values). You might need to split that into two bytes (2 ^ 16) using something equivalent to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/bits-and-bytes\/highbyte\/\"><code>highByte()<\/code><\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/functions\/bits-and-bytes\/lowbyte\/\"><code>lowByte()<\/code><\/a> in Processing before sending and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/variables\/conversion\/wordcast\/\"><code>word()<\/code><\/a> in Arduino (or equivalent STM32 Dev tools if not using Arduino).<\/li>\n<\/ul>\n<p>For example:<\/p>\n<pre><code>void serialWriteWord(Serial port,int value){\n  port.write(highByte(value));\n  port.write(lowByte(value));\n}\n\nbyte lowByte(int word){\n  return (byte)(word &amp; 0xff);\n}\n\nbyte highByte(int word){\n  return (byte)(word &gt;&gt; 8);\n}\n<\/code><\/pre>\n<p>The serial communication part is a totally separate issue (for another question)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">7<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why is the comma in two text fields put at the same time without focusing? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Why is the comma placed without focusing text fields? Because you are using the global keyPressed() event. This condition: if (e.getKey() == &#8216;,&#8217;) checks that the , key is pressed and it&#8217;s redundant to check twice in your case. It&#8217;s equivalent to this simpler\/cleaner snippet: public void keyPressed(KeyEvent e) { if (key == &#8216;,&#8217;){ &#8230; <a title=\"[Solved] Why is the comma in two text fields put at the same time without focusing?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\" aria-label=\"More on [Solved] Why is the comma in two text fields put at the same time without focusing?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2371,323,2370],"class_list":["post-8361","post","type-post","status-publish","format-standard","hentry","category-solved","tag-control-p5","tag-java","tag-processing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Why is the comma placed without focusing text fields? Because you are using the global keyPressed() event. This condition: if (e.getKey() == &#039;,&#039;) checks that the , key is pressed and it&#8217;s redundant to check twice in your case. It&#8217;s equivalent to this simpler\/cleaner snippet: public void keyPressed(KeyEvent e) { if (key == &#039;,&#039;){ ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-12T23:28:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why is the comma in two text fields put at the same time without focusing?\",\"datePublished\":\"2022-09-12T23:28:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\"},\"wordCount\":392,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"control-p5\",\"java\",\"processing\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\",\"name\":\"[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-12T23:28:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why is the comma in two text fields put at the same time without focusing?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb","og_description":"[ad_1] Why is the comma placed without focusing text fields? Because you are using the global keyPressed() event. This condition: if (e.getKey() == ',') checks that the , key is pressed and it&#8217;s redundant to check twice in your case. It&#8217;s equivalent to this simpler\/cleaner snippet: public void keyPressed(KeyEvent e) { if (key == ','){ ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/","og_site_name":"JassWeb","article_published_time":"2022-09-12T23:28:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why is the comma in two text fields put at the same time without focusing?","datePublished":"2022-09-12T23:28:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/"},"wordCount":392,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["control-p5","java","processing"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/","url":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/","name":"[Solved] Why is the comma in two text fields put at the same time without focusing? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-12T23:28:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-is-the-comma-in-two-text-fields-put-at-the-same-time-without-focusing\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why is the comma in two text fields put at the same time without focusing?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8361"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8361\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}