{"id":4453,"date":"2013-05-06T07:00:00","date_gmt":"2013-05-06T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/05\/06\/reading-mouse-input-from-a-console-program-and-programmatically-turning-off-quick-edit-mode\/"},"modified":"2013-05-06T07:00:00","modified_gmt":"2013-05-06T07:00:00","slug":"reading-mouse-input-from-a-console-program-and-programmatically-turning-off-quick-edit-mode","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130506-00\/?p=4453","title":{"rendered":"Reading mouse input from a console program, and programmatically turning off Quick Edit mode"},"content":{"rendered":"<p><P>\nToday&#8217;s Little program shows how to read mouse input from\na console program.\nYou might use this if you are writing a console-mode text\neditor with mouse support,\nor maybe you want to want to add mouse support to your\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/ericlippert\/archive\/2011\/12\/12\/shadowcasting-in-c-part-one.aspx\">\nroguelike game<\/A>.\n<\/P>\n<P>\nBut I&#8217;m not going to implement the game itself.\nInstead, I&#8217;m just going to print mouse coordinates to the\nscreen.\n<\/P>\n<PRE>\n#define <A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/02\/12\/71851.aspx\">UNICODE<\/A>\n#define _UNICODE\n#include &lt;windows.h&gt;\n#include &lt;tchar.h&gt;\n#include &lt;stdio.h&gt;<\/p>\n<p>int __cdecl _tmain(int argc, PTSTR argv[])\n{\n HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);\n BOOL fContinue = TRUE;\n DWORD dwEvents;\n INPUT_RECORD input;\n while (fContinue &amp;&amp;\n        ReadConsoleInput(hConsole, &amp;input, 1, &amp;dwEvents) &amp;&amp;\n        dwEvents &gt; 0) {\n  switch (input.EventType) {\n  case KEY_EVENT:\n   if (input.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) {\n    fContinue = FALSE;\n   }\n  case MOUSE_EVENT:\n   _tprintf(TEXT(&#8220;X=%d, Y=%d; buttons=%x, shift=%x, flags=%x\\n&#8221;),\n     input.Event.MouseEvent.dwMousePosition.X,\n     input.Event.MouseEvent.dwMousePosition.Y,\n     input.Event.MouseEvent.dwButtonState,\n     input.Event.MouseEvent.dwControlKeyState,\n     input.Event.MouseEvent.dwEventFlags);\n   break;\n  }\n }\n return 0;\n}\n<\/PRE>\n<P>\nOur program just loops around collecting input,\nand if it gets a mouse event, it just prints the coordinates.\n&#8220;Insert game here.&#8221;\nIf the user presses the\n<KBD>Esc<\/KBD> key, then we exit.\n<\/P>\n<P>\nRun this program,\nmove the mouse around the window, and&#8230; hey,\nnothing happened!\n<\/P>\n<P>\nOh right, because we forgot to enable mouse input.\nLet&#8217;s try that again.\n<\/P>\n<PRE>\n &#8230;\n HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);<\/p>\n<p> <FONT COLOR=\"blue\">DWORD dwPreviousMode = 0;\n GetConsoleMode(hConsole, &amp;dwPreviousMode);\n DWORD dwNewMode = dwPreviousMode | ENABLE_MOUSE_INPUT;\n SetConsoleMode(hConsole, dwNewMode);\n }<\/FONT><\/p>\n<p> BOOL fContinue = TRUE;\n &#8230;\n }<\/p>\n<p> <FONT COLOR=\"blue\">SetConsoleMode(hConsole, dwPreviousMode);<\/FONT><\/p>\n<p> return 0;\n}\n<\/PRE>\n<P>\nRemember, this is just a Little Program,\nso there is little to no error checking.\n<\/P>\n<P>\nOkay, now you can run the program,\nand as you move the mouse around the window, you get&#8230;\nWell, it depends.\nSome of you may get output,\nand others may get nothing.\n<\/P>\n<P>\nThose of you who got nothing aren&#8217;t getting anything\nbecause you set <I>Quick Edit<\/I> mode on your console.\n<I>Quick Edit<\/I> mode commandeers the mouse and uses it\nfor copy\/paste operations rather than passing it through\nto the application.\nIt&#8217;s handy if you spend most of your time using programs\nthat don&#8217;t use the mouse,\nsince it saves you from having to go to the\n<I>Edit<\/I> menu all the time.\n<\/P>\n<P>\nIt&#8217;s not so handy if you&#8217;re running a program that actually\nwants to use the mouse.\n<\/P>\n<P>\nAdd another line of code to the program to disable\nQuick-Edit mode:\n<\/P>\n<PRE>\n DWORD dwNewMode = dwPreviousMode | ENABLE_MOUSE_INPUT;\n <FONT COLOR=\"blue\">dwNewMode &amp;= ~ENABLE_QUICK_EDIT_MODE;<\/FONT>\n SetConsoleMode(hConsole, dwNewMode);\n<\/PRE>\n<P>\nOkay, now when you run the program and move the mouse around,\nyou get&#8230; still nothing.\n<\/P>\n<P>\nAh, because\n<CODE>ENABLE_QUICK_EDIT_MODE<\/CODE> is an extended\nflag,\nand if you want to modify an extended flag, you also have to pass\nthe\n<CODE>ENABLE_EXTENDED_FLAGS<\/CODE> flag.\n(You can guess how I discovered this.)\n<\/P>\n<PRE>\n dwNewMode &amp;= ~ENABLE_QUICK_EDIT_MODE;\n SetConsoleMode(hConsole, dwNewMode <FONT COLOR=\"blue\">|\n                          ENABLE_EXTENDED_FLAGS<\/FONT>);<\/p>\n<p> &#8230;\n SetConsoleMode(hConsole, dwPreviousMode <FONT COLOR=\"blue\">|\n                          ENABLE_EXTENDED_FLAGS<\/FONT>);\n<\/PRE>\n<P>\nOkay, now you can run the program,\nand as you wave the mouse around,\nthe coordinates will be printed to the screen.\nWhew.\n<\/P>\n<P>\n<B>Exercise<\/B>:\nDiscuss why there is the crazy\n<CODE>ENABLE_EXTENDED_FLAGS<\/CODE> flag.\nFor bonus points, come up with a way the flag could have\nbeen avoided while still solving the problem the flag was created for.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little program shows how to read mouse input from a console program. You might use this if you are writing a console-mode text editor with mouse support, or maybe you want to want to add mouse support to your roguelike game. But I&#8217;m not going to implement the game itself. Instead, I&#8217;m just going [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-4453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little program shows how to read mouse input from a console program. You might use this if you are writing a console-mode text editor with mouse support, or maybe you want to want to add mouse support to your roguelike game. But I&#8217;m not going to implement the game itself. Instead, I&#8217;m just going [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=4453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/4453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=4453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=4453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=4453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}