How to get Mouse input inside a C console program on Windows-10?

丶灬走出姿态 提交于 2020-01-03 05:46:09

问题


I need to interact with user's mouse input in my simple C console program. I did some research and found a related Microsoft's docs on their page here. For a start, I copied all of their sample-code and pasted that into my editor. Upon compilation, it gets compiled well, with a nice little '.exe' which when run, tells/prints correctly all 'Key events' and 'resize events', But No Mouse event! How to successfully get mouse input inside C console program? My MCVE is the given sample-code here on Microsoft site, as i used that sample-code verbatim! I'm using Windows 10 Pro (1703), just in case.


EDIT: - It appears to be a problem which is not unique to me. Another StackOverflow user also reported the same problem here on his Windows-10 system. His provided MCVE was also working on Windows-7, but not on Windows-10, just like my quoted-MCVE on Microsoft's site as a sample-code might be working on older Windows-versions, but not on windows-10, where 'Quick Edit Mode' seemingly is enabled in 'Console-Defaults'.


回答1:


After spending hours i have found the culprit for failure of that quoted-in-my-question Microsoft's official sample code here. And the culprit is the very console itself! Actually it's a 'setting' namely 'Quick Edit Mode' for the 'Console Windows' .

You can access it by:

  1. Opening command-prompt and right-clicking on its title-bar and then clicking 'Defaults'
  2. A Dialog box would appear, titled 'Console Windows Properties'. There in Options tab, under Edit Options sub-heading, you would find 'Quick Edit Mode' checkbox!

I have attached screen-captures below:

The problem was being caused by that 'Quick Edit Mode' option which was enabled(checkbox is checked) by default on my Windows 10. And in this enabled status, this 'Quick Edit Mode' was consuming all the Mouse-Events and wasn't dispatching any to my '.exe' .

When this 'Quick Edit Mode' options' checkbox is unchecked (disabled), then program runs fine as intended/coded in that sample-code here, telling/printing all Mouse events. [ Phew! ]

NOTE: The change in 'Console Properties' requires relaunch of the console, to take effect.


EDIT: IMPROVED 'PORTABLE' SOLUTION!

The solution above is not 'portable'. That's just for the local-machine and of course, it's also 'manual work'. To make that sample-code work without requiring user to disable Quick Edit Mode 'manually' as explained above, we can disable Quick Edit Mode programmatically by adding following lines of code inside that sample-code

        /* 
           Step-1:
           Disable 'Quick Edit Mode' option programmatically
        */
        fdwMode = ENABLE_EXTENDED_FLAGS;
        if (! SetConsoleMode(hStdin, fdwMode) )
             ErrorExit("SetConsoleMode");
        /* 
           Step-2:
           Enable the window and mouse input events,
           after you have already applied that 'ENABLE_EXTENDED_FLAGS'
           to disable 'Quick Edit Mode'
        */
        fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
        if (! SetConsoleMode(hStdin, fdwMode) )
            ErrorExit("SetConsoleMode");

I found about that 'ENABLE_EXTENDED_FLAGS' option on Microsoft's docs about SetConsoleMode()

After we have applied ENABLE_EXTENDED_FLAGS in SetConsoleMode() function, our program would receive/print all 'Mouse Events' even though the user has 'Quick Edit Mode' option enabled in Console Defaults options. And after our program has finished doing its job (receiving Mouse events in this super simple scenario) user's Console Defaults would be undisturbed because before our program quits, it would restore user's original (saved in the start of our program) Console Mode as follows:

         /* Restore original console mode before exit. */
         SetConsoleMode(hStdin, fdwSaveOldMode);

As done in that sample-code there, which works well, but fails to print Mouse events if user has 'Quick Edit Mode' enabled in his console. Therefore, to make that sample-code work in scenarios where user's Console Defaults has 'Quick Edit Mode' enabled, we should include/put inside that sample-code the code-snippet (of applying ENABLE_EXTENDED_FLAGS) as shown in this 'EDIT' section above.



来源:https://stackoverflow.com/questions/46795072/how-to-get-mouse-input-inside-a-c-console-program-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!