Jump to content
  • Advertisement
Sign in to follow this  

Sending simulated keystrokes to Q2 source

This topic is 4973 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I think Q2 uses its own values for key strokes...
probly defined somewhere

Share this post

Link to post
Share on other sites
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 ....

Share this post

Link to post
Share on other sites
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
[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.
This parameter is not used.
[in] Specifies various aspects of function operation. This parameter can be one or more of the following values.
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
If specified, the key is being released. If not specified, the key is being depressed.
[in] Specifies an additional value associated with the key stroke.

Are you saying the bScan parameter is used?


Share this post

Link to post
Share on other sites
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 unique
keybd_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.

Share this post

Link to post
Share on other sites
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)


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

Am I just not using the right data types?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!