如何在Windows-10的C控制台程序中获取鼠标输入? [英] How to get Mouse input inside a C console program on Windows-10?

查看:158
本文介绍了如何在Windows-10的C控制台程序中获取鼠标输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在简单的C控制台程序中与用户的鼠标输入进行交互。我进行了一些研究,发现



问题是由我在Windows 10上默认启用了快速编辑模式 选项(选中此复选框)。在此启用状态下,此快速编辑模式 消耗了所有鼠标事件,并且没有向我的'.exe'发送任何事件。



当此快速编辑模式 选项复选框为取消选中(禁用),然后程序按预期/编码正常运行,其中此处为示例代码,告诉/打印所有Mouse事件。 [Ph! ]



注意:控制台属性中的更改需要重新启动控制台才能生效。






编辑:改进的便携式解决方案!



上述解决方案不是便携式的。这仅适用于本地计算机,当然,也是手动工作。为了使示例代码在没有要求用户禁用 快速编辑模式 手动 ,如上所述,我们可以禁用 快速编辑通过在示例代码

  / * 
步骤1:
禁用快速编辑模式选项以编程方式
* /
fdwMode = ENABLE_EXTENDED_FLAGS;
if(!SetConsoleMode(hStdin,fdwMode))
ErrorExit( SetConsoleMode);
/ *
步骤2:
启用窗口和鼠标输入事件,在您已应用 ENABLE_EXTENDED_FLAGS
禁用快速编辑模式后,

* /
fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
if(!SetConsoleMode(hStdin,fdwMode))
ErrorExit( SetConsoleMode);

我发现有关 ENABLE_EXTENDED_FLAGS 选项在 Microsoft关于 SetConsoleMode()



SetConsoleMode()中应用 ENABLE_EXTENDED_FLAGS 之后/ code>函数,即使用户在控制台默认值选项中启用了快速编辑模式选项,我们的程序仍会接收/打印所有鼠标事件。在我们的程序完成工作(在这种超级简单的情况下接收鼠标事件)后,用户的控制台默认值将不受干扰,因为在我们的程序退出之前,它将恢复用户的原始设置(保存在我们的开始位置)程序)控制台模式,如下所示:

  / *退出前恢复原始控制台模式。 * / 
SetConsoleMode(hStdin,fdwSaveOldMode);

此处的示例代码效果很好,但如果用户有失败则无法打印鼠标事件在他的控制台中启用了快速编辑模式。因此,要使该示例代码要在用户的控制台默认值启用了快速编辑模式的情况下工作,我们应该在示例代码代码段(应用 ENABLE_EXTENDED_FLAGS ),如下所示上面的编辑 部分。


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:

  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.

这篇关于如何在Windows-10的C控制台程序中获取鼠标输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆