Why does not keybd_event work in some contexts?

3

I want to understand why keybd_event does not work in some contexts. For example, it does not work in games with League of Legends or games emulated in ePSXe.

The following code:

Keys key = Keys.Q;

keybd_event((byte)key, 0x45, 0x0001 | 0, 0);
keybd_event((byte)key, 0x45, 0x0001 | 0x0002, 0);

It works in games like Terraria, but does not work in the games I mentioned earlier.

However, I noticed that if I try to hit Esc on ePSXe, it works and I'm taken to the main screen. The command has activated an emulator action, but does not activate game actions. I believe the same thing happens in League of Legends.

Why does not it work exactly? Is there maybe a way to make this work?

    
asked by anonymous 26.12.2013 / 20:32

1 answer

3

First you should only use the flag 0x0001 (KEYEVENTF_EXTENDEDKEY) when you want to specify that the key was pressed on the numeric keypad.

Second there are two modes of an application receiving keyboard input:

  • Processing your WM_KEYUP and WM_KEYDOWN messages from your message stack
  • Checking the status of the keys in real time

Common Windows applications generally use the first option, since games generally use the second one because they only concern the keys that are pressed at the time and not the one pressed 100 milliseconds ago.

What the keybd_event function does is create a WM_KEYUP or WM_KEYDOWN message with the key you pressed and play in the message stack of the window that is currently focused, if the application in question does not receive its entries through messages keybd_event will not work.

    
18.02.2014 / 22:10