Sending simulated keystrokes to Q2 source

Started by
5 comments, last by pr0fess0r 19 years, 2 months ago
Hi I'm developing a small project to control Quake 2 using another application (written in VB.NET). This application creates keyboard events using the keybd_event() method. So for example, I create keybd_event(VK_LEFT, 0, 0, 0) To simulate the left arrow being pressed down, and keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0) when the key is released. In Windows applications, this works fine (I can navigate around a block of text in Notepad fine, etc). But Quake 2 does not recognise the keystrokes - it jumps straight to the menu. In keys.c I added this code to output the key Quake 2 was receiving to the display: Com_Printf ("%s (%d) HAS BEEN PRESSED.\n", Key_KeynumToString (key),key ); in the function key_event() This works fine, but when simulated keys are sent, I get <UNKNOWN KEYNUM> (0) in the display Any idea how to send keystrokes to Quake 2 and have it treat them as keyboard input?
Advertisement
I think Q2 uses its own values for key strokes...
probly defined somewhere
You need to specify a scan code value. I dont recall off the top of my head how you get this value, but I will check when I get home and if you still haven't worked things out I will show you what I did. The thing you need to be aware of with the keybd_event() is that it will work fine with no scan code on MOST applications, the tendency being functional on applications made recently. I had to learn this the hard way when I could get it to dump text into Notepad just fine, but nothing worked when trying to type into a Citrix terminal.

I am pretty sure that will make it work, IIRC Quake 2 was made for windows so it wont be doing too much funny stuff with keyboard input. I think ....
Hi
Do you mean pass a scan code value as one of the keybd_event parameters?
I have
Private Const VK_RIGHT = &H27
for example. According to MSDN
VOID keybd_event(      

    BYTE bVk,
    BYTE bScan,
    DWORD dwFlags,
    PTR dwExtraInfo
);
Parameters
bVk
[in] Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual-Key Codes.
bScan
This parameter is not used.
dwFlags
[in] Specifies various aspects of function operation. This parameter can be one or more of the following values.
KEYEVENTF_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP
If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
[in] Specifies an additional value associated with the key stroke.

Are you saying the bScan parameter is used?

cheers
Yes, it is used. Trust me on this one, I dont know why it is listed as not used, my only guess is that it is now depreciated. I mean after all, SendKeys() doesnt use it, but then again SendKeys() wont send to certain stuff.

// The three functions I used were from user32.dll// for something like enter, where there are multiple enter keys (same as for tab)keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN,0), 0, 0);keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN,0), KEYEVENTF_KEYUP, 0);// for a character key, that is uniquekeybd_event(keyCode, (byte)OemKeyScan(keyChar), 0, 0);keybd_event(keyCode, (byte)OemKeyScan(keyChar), 0, KEYEVENTF_KEYUP);


You can find the keycode table along with more info in general just by googling. I am sorry I cant give more info, I couldnt find my code so I had to go off what I could remember and a quick google search. But the MapVirtualKey and OemKeyScan were the things that made it work.
Wow, thats great - thank you. I'll give it a try and let you know how it goes ;)
Hi
I gave that a try but am running into problems:
This works fine (but doesnt send the data that Q2 needs):

keybd_event(VK_LEFT, 0, 0, 0)
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0)

but if I use this, nothing happens:

keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), 0, 0)
keybd_event(VK_LEFT, MapVirtualKey(VK_LEFT, 0), KEYEVENTF_KEYUP, 0)

where

Private Const VK_LEFT = &H25
Private Const KEYEVENTF_KEYUP = &H2

Am I just not using the right data types?
cheers

This topic is closed to new replies.

Advertisement