在数字键盘上发送特定键,如 +、-、/或 Enter(模拟按键) [英] Sending specific keys on the Numpad like +, -, / or Enter (simulating a keypress)

查看:150
本文介绍了在数字键盘上发送特定键,如 +、-、/或 Enter(模拟按键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个项目,该项目需要模拟按键操作以在不同的应用程序中引起特定行为.

I am working on a project where it is necessary to simulate key-presses to cause specific behaviours in a different application.

一切运行良好,使用正在导入的 keybd_event 函数(可能有更好的方法,但工作正常).

All is running well and fine, using the keybd_event function that is being imported (there might be better ways, but it is working fine).

现在我想添加对所有小键盘的特定支持.

Now I want to add specific support for all of the numpad.

看着 e.G.这里 http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx 或在 System.Windows.Input.Key 命名空间中,我可以轻松找到 Num0..Num9 和 NumLock 的键.但是.. 我找不到 Num/、Num+、NumEnter 等的任何东西.

Looking e. g. here http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx or in the System.Windows.Input.Key namespace, I can easily find keys for Num0..Num9, as well as for NumLock. But.. I cannot find anything for Num/, Num+, NumEnter etc.

我编写了一个快速的 froms 应用程序来捕获 keydown 事件,输出事件参数,并得到了一些有趣的结果:

I wrote a quick froms app to catch the keydown event, outputting the event paramters, and got some interesting results:

e.KeyCode NumLock e.KeyData NumLock e.KeyValue 144 e.Modifiers None  
e.KeyCode Divide e.KeyData Divide e.KeyValue 111 e.Modifiers None  
e.KeyCode Multiply e.KeyData Multiply e.KeyValue 106 e.Modifiers None  
e.KeyCode Subtract e.KeyData Subtract e.KeyValue 109 e.Modifiers None  
e.KeyCode Add e.KeyData Add e.KeyValue 107 e.Modifiers None  
e.KeyCode NumLock e.KeyData NumLock e.KeyValue 144 e.Modifiers None  
e.KeyCode NumLock e.KeyData NumLock e.KeyValue 144 e.Modifiers None  
e.KeyCode Divide e.KeyData Divide e.KeyValue 111 e.Modifiers None  
e.KeyCode Multiply e.KeyData Multiply e.KeyValue 106 e.Modifiers None  
e.KeyCode Subtract e.KeyData Subtract e.KeyValue 109 e.Modifiers None  
e.KeyCode Add e.KeyData Add e.KeyValue 107 e.Modifiers None  
e.KeyCode Return e.KeyData Return e.KeyValue 13 e.Modifiers None

Num+ 键(等等)似乎是 Windows 调用功能键的键(如 Num+ 键的 F18).所以..这很奇怪,但没关系.

The Num+ Key (and so on) seem to be keys that Windows calls function keys (like F18 for the Num+ key). So.. that is strange, but ok.

但是.. 我无法区分 Enter 键和 NumEnter 键.这些对于我的应用程序来说是不同的,所以我必须为两者发送特定的密钥代码.

But.. I cannot distinguish the Enter-Key from the NumEnter Key. Those are different for my application, so I have to send specific key-codes for both.

这就是我的问题:如何发送普通的回车键以及如何发送 NumEnter 键?

And that is my question: how can I send an ordinary enter-key and how can I send a NumEnter key?

(我不知道这是否有什么区别,我使用的是德语键盘布局.)

(I don't know whether it makes any difference, I am on a German keyboard layout.)

感谢您的任何想法!

推荐答案

既然你在谈论一个相反的解决方案,检测事件,我想提出它,我什至不必覆盖 WndProc.我可以简单地发送我自己的消息.

Since you are talking about a the-other-way-round solution, detecting the event, and I want to raise it, I don't even have to override the WndProc. I can simply send my own messages.

根据您的解决方案,我查看了 SendMessage/PostMessage,然后是 WM_KEYDOWN 和 WM_KEYUP.文档实际上为您提供了信息(如果您真的很难看的话).

From your solution, I had a look at SendMessage/PostMessage, and then WM_KEYDOWN and WM_KEYUP. The documentation actually gives you the info (if you look really really hard).

http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms646281(v=vs.85).aspx

所以我的解决方案(编译,现在找到正确的窗口(输入文本的位置))是这样的:

So my solution (compiles and now with finding the right window (where to enter the text)) is like this:

 bool keyDown = true; // true = down, false = up
 const uint WM_KEYDOWN = 0x0100;
 const uint WM_KEYUP = 0x0101;
 const int VK_RETURN = 0x0D;

 IntPtr handle = IntPtr.Zero;
 // Obtain the handle of the foreground window (active window and focus window are only relative to our own thread!!)
 IntPtr foreGroundWindow = GetForegroundWindow();
 // now get process id of foreground window
 uint processID;
 uint threadID = GetWindowThreadProcessId(foreGroundWindow, out processID);
 if (processID != 0)
 {
 // now get element with (keyboard) focus from process
 GUITHREADINFO threadInfo = new GUITHREADINFO();
 threadInfo.cbSize = Marshal.SizeOf(threadInfo);
 GetGUIThreadInfo(threadID, out threadInfo);
 handle = (IntPtr)threadInfo.hwndFocus;
 }

 int lParam = 1 << 24; // this specifies NumPad key (extended key)
 lParam |= (keyDown) ? 0 : (1 << 30 | 1 << 31); // mark key as pressed if we use keyup message
 PostMessage(handle, (keyDown) ? WM_KEYDOWN : WM_KEYUP, VK_RETURN, lParam); // send enter

希望它对其他人也有用.正如 Vendetta 给我的提示一样.

Hope it is useful to someone else as well. As was Vendetta's tip to me.

如果你有更好的解决方案,请说出来!

And.. if you do have a better solution, please say so!

这篇关于在数字键盘上发送特定键,如 +、-、/或 Enter(模拟按键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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