按键 - 事件生成 [英] Key pressed - event generated

查看:82
本文介绍了按键 - 事件生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户按下某个键时,如何生成一个事件(即使我当前的应用程序不在焦点上)?我实际上希望根据用户按下的键执行一些代码。我的应用程序将在后端运行。期待任何帮助。

How I can get an event to be generated whenever the user presses a key(even if my current application is not in focus)? I actually wish to execute some code according to the keys pressed by the user. My application will be running in back end. Looking forward to any help.

推荐答案

您好,



使用键盘挂钩:

简单的C#全局低级键盘挂钩 [ ^ ]

使用C#中的Hook [ ^ ]
Hi,

Use Keyboard Hooks:
A Simple C# Global Low Level Keyboard Hook[^]
Using Hooks from C#[^]


你应该明白焦点总是意味着键盘焦点,而不是别的。这意味着,当用户按下键盘上的键时,只有具有焦点的控件才会接收输入事件。



解决方案1解释了如何通过键盘触发某些事件Windows Hook,但这将是对正常UI开发的真正滥用。使用这种钩子只能用于某些特殊情况。例如,它可以是键盘宏,系统全局实用程序等的记录。如果这只是一个单独的UI应用程序,它没有干预其他进程的行为,你应该更好地检查你的UI设计。



-SA
You should understand that "focus" always means "keyboard focus" and nothing else. It means, only the control which has focus receives input events when the user presses key on a keyboard.

Solution 1 explains how you can trigger some events via a keyboard Windows Hook, but that would be a real abuse of normal UI development. Using such hooks can be justified only for some special cases. For example, it could be recording of keyboard macro, system-global utilities and the like. If this is just the separate UI application which does not have a task of intervention into the behavior of other processes, you should rather review your UI design.

—SA


请参阅 WinForms和WPF中的全局快捷方式 [ ^ ]


这篇关于按键 - 事件生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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