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

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

问题描述

我工作的一个项目,这是必要的,以模拟键盘presses导致具体行为在不同的应用程序。

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.

展望如G。在这里 http://msdn.microsoft.com/en -us /库/ dd375731(V = VS.85)的.aspx 或System.Windows.Input.Key命名空间中,我可以很容易地找到密钥Num0..Num9,以及为NumLock键。但是..我无法找到任何东西民/,数+,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.

我写了一个快速320交织的应用程序,以赶上keydown事件,输出事件paramters,并得到了一些有趣的结果:

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

的数+密钥(等)似乎是键是Windows调用功能键(如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关键。这些都是我的应用程序不同,所以我要发送特定的键盘codeS两种。

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.)

THX的任何想法!

推荐答案

既然你是在谈论一个最另一路的全方位解决方案,检测的情况下,我想提高它,我甚至不有重写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-美国/库/ 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

希望它是有用的其他人也是如此。由于是仇杀的提示我。

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!

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

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