Why does keybd_event not work in some contexts?

Asked

Viewed 304 times

3

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

The following code:

Keys key = Keys.Q;

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

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

However, I noticed that if I try to squeeze Esc in ePSXe, it works and I am 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 doesn’t it work exactly? There is perhaps a way to make it work?

  • It is worth remembering that this function is native and not .Net. According to MSDN it was also exceeded by SendInput. http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304%28v=vs.85%29.aspx This will probably not solve your "problem", as it may be related to how the application gets user data entry.

  • 4

    It is probably because the applications (games) in question must be reading the keyboard directly on the device and not using the Windows API. If they are using Directx for keyboard reading, Directx may read directly from the device. It is worth testing a simple example of Directx keyboard reading and testing your program.

  • @Brunolm can you clarify more context? Are you under Directx? Use XNA? Already checked the Win32 keyboard events?

1 answer

3

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

Second there are two ways an application receives keyboard input:

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

Common Windows applications usually use the first option, whereas games usually use the second because they only care about the keys that are pressed at that time and not the one that was pressed 100 milliseconds ago.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.