Sendkey Function Enter Key Issue

1.7k views Asked by At

So here is my problem , I've been trying to simulate various key events with the sendinput() function, I even wrote functions to simulate complex key events like winkey+D, alt+f4 etc using that function , anyway everything worked fine till I decided to try out the enter key aka return with virtual keycode VK_RETURN as provided by this nice msdn list of VK codes, the thing is that it doesn't work

now, i am guessing that 3 things can be wrong with this enter key ,

  1. the virtual key code for enter and the value that the pc keyboard actually uses are not the same

  2. there is something wrong in the code typos wrong codes , bad calls etc; something i'm not so sure since send input works for the rest of the keys tested so far

  3. the enter, return carriage, return or whatever the superstar of keyboard keys called just doesn't work like the other buttons so my code is not right for it

note: if its the 1 , can you pls enlighten on how do i get the right value of a keycode, programmatically for a given button without relying on static resources like msdn vk list

anyway here is my snippet

i make an input struct like this and assign it as keyboard input

             INPUT Keybord;
         Keybord.type = INPUT_KEYBOARD;
         Keybord.ki.wScan = 0;
         Keybord.ki.time = 0;
         Keybord.ki.dwExtraInfo = 0;

here i assign a button VK, i use a struct because i simulate complex events too
in this case the the key press simulation function that follows works just fine and a tab is pressed

 cndex[38].primary = VK_TAB;

now here is the troublemaker,i assign it this way

 cndex[39].primary =  VK_RETURN;

here is my key click function , the first call presses the and the second releases it

        void Simple_press(int i){
     Keybord.ki.wVk = cndex[i].primary;
     Keybord.ki.dwFlags = 0;
     SendInput(1, &Keybord, sizeof(INPUT));

     Keybord.ki.dwFlags = KEYEVENTF_KEYUP;
     SendInput(1, &Keybord, sizeof(INPUT));

 };

another note is that i don't want to use the sendKeys function , its sluggish and it doesn't work for "{ENTER}" argument either

2

There are 2 answers

1
Dave On

If your app has a window then its possible to use the spy++ tool to see what data the SendInput command is sending and if the target also has a window you can use the same tool to see if and what messages are being sent to it. Its a handy tool for this very purpose - it may help or at least steer you in the right direction as to what's going on.

http://msdn.microsoft.com/en-us/library/aa264396%28v=vs.60%29.aspx

0
George mits On

Alright after some short investigation with spy++ it seems that the virtual code for enter is different in my keyboard , in fact it list as some illusive PA1 a placeholding key used by ibm mainframes, and I debug my in a mainframe like this, anyway , I searched around and I learned that you can just use the mapvirtualkey() to convert scan codes to vk's that are more reliable and GetKeyboardLayout()(same section) that helps to get the type of my keyboard and work with it without relying on static resources.

here is my spy++ log first goes the fake one , then the real enter keypress ,on another note, the real enter event sends a character message too ,the "\n" as we know it, it seems that the char message is not extracted by the process that receives it using the original key event message as msdn suggest at in the enter key case

<000006> 000804EA P WM_KEYDOWN nVirtKey:VK_PA1 cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 <000008> 000804EA P WM_KEYUP nVirtKey:VK_PA1 cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0

<000014> 000804EA P WM_KEYDOWN nVirtKey:VK_RETURN cRepeat:1 ScanCode:1C fExtended:0 fAltDown:0 <000015> 000804EA P WM_CHAR chCharCode:'13' (13) cRepeat:1 ScanCode:1C fExtended:0 fAltDown:0