Sending specific keys to Numpad, for example, +, -, / or Enter (simulating a keystroke)

I am working on a project where you need to simulate keystrokes to trigger specific behaviors in another application.

Everything works fine and fine using the imported keybd_event function (maybe there are better ways, but it works fine).

Now I want to add some support for all the number keys.

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

I wrote a quick froms application to catch a keydown event, output event parameters and get 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 + Key (and so on) appear to be keys that Windows calls function keys (for example, F18 for the Num + key). So .. this is strange, but normal.

But .. I can not distinguish Enter-Key from NumEnter Key. They are different for my application, so I have to send specific keycodes for both.

And this is my question: how can I send a regular input key and how can I send a NumEnter key?

(I don't know if any value matters, I'm on a German layout.)

Thanks for any ideas!

+6
source share
3 answers

Since you are talking about the solution β€œround”, detecting the event, and I want to raise it, I don’t even need to redefine WndProc. I can just send my messages.

From your solution, I looked at SendMessage / PostMessage, and then WM_KEYDOWN and WM_KEYUP. The documentation actually gives you information (if you really look very 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 (compiled and now with the search for the correct window (where to enter text)) is as follows:

  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 this is useful for someone else as well. Like Vendetta, he attacked me.

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

0
source

I found that works here for me!

 protected override void WndProc(ref Message m) { if (m.Msg == 256 && m.WParam.ToInt32() == 13) { // WM_KEYDOWN == 256, Enter == 13 if ((m.LParam.ToInt32() >> 24) == 0) { MessageBox.Show("main enter pressed!"); } else { MessageBox.Show("numpad enter pressed!"); } } else { base.WndProc(ref m); } } 
+3
source

thanks andreas for providing a start solution. here is a more complete version:

 [DllImport("user32.dll")] private static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("user32.dll")] private static extern bool GetGUIThreadInfo(uint idThread, out GUITHREADINFO lpgui); public struct GUITHREADINFO { public int cbSize; public int flags; public int hwndActive; public int hwndFocus; public int hwndCapture; public int hwndMenuOwner; public int hwndMoveSize; public int hwndCaret; public System.Drawing.Rectangle rcCaret; } private void sendNumpadEnter() { 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 } 
+1
source

Source: https://habr.com/ru/post/888012/


All Articles