{"id":6304,"date":"2024-03-23T00:01:44","date_gmt":"2024-03-23T07:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6304"},"modified":"2024-03-30T08:35:08","modified_gmt":"2024-03-30T15:35:08","slug":"whats-next-keyboard","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6304","title":{"rendered":"What&#8217;s Next, Keyboard?"},"content":{"rendered":"<p>Suppose you must write code that remains busy while checking to see whether a key has been pressed. The program repeats a loop, performing various tasks, but eager for that key press. When a key is pressed, the code fetches the key. Two things stand in your way to make this happen.<br \/>\n<!--more--><br \/>\nFirst, standard input is streaming. True, input can be buffered or unbuffered, but it all comes in the stream not directly from the keyboard.<\/p>\n<p>Second, C library functions that read standard input are blocking calls. They wait for input, pausing the entire program.<\/p>\n<p>The process I&#8217;m referencing is called <em>polling<\/em>. Activity takes place, such as in a loop, and certain conditions are checked. If one of these conditions is whether a key has been pressed on the keyboard, how can you check it without pausing the entire program?<\/p>\n<p>While it may seem impossible to perform a &#8220;just checking&#8221; routine on standard input, it can be done. But first I must present a silly example.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03_23-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_03_23-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char a;\r\n\r\n    a = 'A';\r\n    while(1)\r\n    {\r\n        printf(\" %c\",a++);\r\n        if( a=='Z' )\r\n            a = 'A';\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code builds a program that continuously outputs uppercase letters A through Z. As it doesn&#8217;t do anything else, it runs fast, spewing the alphabet all over the terminal window. Press Ctrl+C to terminate it.<\/p>\n<p>The program&#8217;s endless <em>while<\/em> loop should have a terminating condition, but it&#8217;s anticipated that the loop terminates when the user presses a key. The problem is how to implement this condition without halting the program?<\/p>\n<p>To prove that it can&#8217;t work (at least for now), the following update to the code calls the <em>getchar()<\/em> function. This function is one way to check on the keyboard, but it halts the entire program:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03_23-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_03_23-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char a;\r\n\r\n    a = 'A';\r\n    while(1)\r\n    {\r\n        printf(\" %c\",a++);\r\n        if( a=='Z' )\r\n            a = 'A';\r\n        <span class=\"comments\">\/* terminate on key *\/<\/span>\r\n        if(getchar())\r\n            break;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>When the <em>getchar()<\/em> function is encountered, the loop stops to wait for keyboard input. In fact, the loop runs only once. This code is a poor example of polling.<\/p>\n<p>One solution is to check standard input at a low level. For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6305\">next week&#8217;s Lesson<\/a>, I show how to access the standard input device in this manner. But other solutions exist as well. For example, network programming involves a lot of polling. I cover these other solutions as I continue to explore checking the keyboard for input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes a program needs to check to see whether a key has been pressed without reading that key. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6304\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6304","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6304"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6304\/revisions"}],"predecessor-version":[{"id":6339,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6304\/revisions\/6339"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}