{"id":10566,"date":"2019-03-02T10:46:09","date_gmt":"2019-03-02T15:46:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10566"},"modified":"2019-03-03T18:12:08","modified_gmt":"2019-03-03T23:12:08","slug":"get-your-key-press","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/02\/get-your-key-press\/","title":{"rendered":"Get your key press"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7465\"><tt>getkey<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870065\">Jos<\/a>.<\/p><p>Here&#8217;s an entry from a long-time user. Honestly speaking, I&#8217;m surprised why this entry has not been Picked yet. It was initially posted in 2005. It&#8217;s a straight-forward tool that accomplishes something that I often need to do, which is to get user keyboard input. When I am developing an app, then I can make use of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.ui.figure-properties.html#buiwuyk-1-KeyPressFcn\"><tt>KeyPressFcn<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.ui.figure-properties.html#buiwuyk-1-WindowKeyPressFcn\"><tt>WindowKeyPressFcn<\/tt><\/a>. But if I don&#8217;t have a figure window, it becomes a bit tricky. Jos&#8217;s <tt>getkey<\/tt> makes it easy for me. It actually creates an invisible figure to capture the key input. It&#8217;s a modal figure so it doesn&#8217;t lose focus. The simplest syntax is this<\/p><pre class=\"language-matlab\">ch = getkey;\r\n<\/pre><p>with additional syntax for extra useful features.<\/p><p>Thanks for making things easier, Jos!<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10566#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7465#comment\">comment<\/a> for Jos.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_8003e48f31bf473c976b3df3d5ff870c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='8003e48f31bf473c976b3df3d5ff870c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 8003e48f31bf473c976b3df3d5ff870c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2019 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_8003e48f31bf473c976b3df3d5ff870c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><\/div><!--\r\n8003e48f31bf473c976b3df3d5ff870c ##### SOURCE BEGIN #####\r\n%%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7465 |getkey|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870065 Jos>.\r\n%\r\n% Here's an entry from a long-time user. Honestly speaking, I'm surprised\r\n% why this entry has not been Picked yet. It was initially posted in 2005.\r\n% It's a straight-forward tool that accomplishes something that I often\r\n% need to do, which is to get user keyboard input. When I am developing an\r\n% app, then I can make use of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.ui.figure-properties.html#buiwuyk-1-KeyPressFcn\r\n% |KeyPressFcn|> or\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.ui.figure-properties.html#buiwuyk-1-WindowKeyPressFcn\r\n% |WindowKeyPressFcn|>. But if I don't have a figure window, it becomes a\r\n% bit tricky. Jos's |getkey| makes it easy for me. It actually creates an\r\n% invisible figure to capture the key input. It's a modal figure so it\r\n% doesn't lose focus. The simplest syntax is this\r\n%\r\n%   ch = getkey;\r\n%\r\n% with additional syntax for extra useful features.\r\n%\r\n% Thanks for making things easier, Jos!\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10566#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7465#comment\r\n% comment> for Jos.\r\n\r\n##### SOURCE END ##### 8003e48f31bf473c976b3df3d5ff870c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro&#8216;s pick this week is getkey by Jos.Here&#8217;s an entry from a long-time user. Honestly speaking, I&#8217;m surprised why this entry has not been Picked yet. It was initially posted in&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/02\/get-your-key-press\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10566"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=10566"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10566\/revisions"}],"predecessor-version":[{"id":10572,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10566\/revisions\/10572"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}