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'.
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:
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
I found about that '
ENABLE_EXTENDED_FLAGS
' option on Microsoft's docs aboutSetConsoleMode()
After we have applied
ENABLE_EXTENDED_FLAGS
inSetConsoleMode()
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: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.