背景按键监听器
我有一个简单的窗口窗体应用程序,当我按空格时打开大写锁定,如果按字母则将其关闭。
问题是我必须专注于窗口才能工作(最顶层也不起作用,最顶层也不聚焦它,只是将窗口显示在所有其他未聚焦的窗口之上)。
任何人都知道即使我在记事本中书写,我怎样才能使其工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按键记录可用于顽皮的事情,并且像这样操纵大写锁定似乎相当奇怪,但由于该信息已经公开可用,并且您比我更了解您的用户故事,所以我发布了一个解决方案。
以下是基于 C# 键盘记录器代码。
将其粘贴到 Visual Studio 中新 Windows 应用程序的 Program.cs 中进行尝试。
如果您拦截按键事件以打开和关闭大写锁定,则该事件将在应用程序处理之前被拦截。这意味着按下字母键时关闭大写锁定将导致您正在键入的应用程序收到小写字母,即使紧接在空格之后也是如此。
我假设您正在尝试强制每个单词中第一个字母的大写(如果是这样,您可能还需要处理其他键,例如 Return ),所以我的代码片段只会在按下下一个键时关闭大写锁定按下字母后发生的事件。请注意,您不能只是尝试捕获该键,因为在快速打字时,您可能会按住第一个键,直到按下下一个键。
Key logging can be used for naughty stuff, and manipulating Caps Lock like that seems rather strange, but since the info is already publicly available, and you know your user stories better than me, I've posted a solution.
Here's an example based on the code snippet posted from keylogger code in C# in the MSDN forum.
Paste that into a new Windows application's Program.cs in Visual Studio to try it out.
If you intercept a key down event to turn Caps Lock on and off, then the event is intercepted before the application handles it. This means that turning Caps Lock off when a letter key is pressed will result in the application you are typing in receiving a lower case letter, even directly after a space.
I've assumed you are trying to force the capitalization of the first letter in each word (and if so, you may need to handle other keys such as Return too), so my snippet will only turn Caps Lock off on the next key down event following a letter being pressed. Note that you can't just try and capture the key up, as when typing fast you may hold the initial key down until after you've pressed the following key.