# Simulate Keypress

This is a discussion on Simulate Keypress within the Windows Programming forums, part of the Platform Specific Boards category; The search function is quite handy, however, I still cannot seem to figure this out. I'm trying to write a ...

1. ## Simulate Keypress

The search function is quite handy, however, I still cannot seem to figure this out. I'm trying to write a little program that sends little daily messages from the server in the game Halo. I have this server and in order to send a message, you have to press 'T' to bring up the chat-box, then type your message, then press enter to send it.

I used the keybd_event function like so..
Code:
keybd_event('T',0,0 , 0);
keybd_event('T',0,KEYEVENTF_KEYUP , 0);
keybd_event('A',0,0 , 0);
keybd_event('A',0,KEYEVENTF_KEYUP , 0);
keybd_event(VK_RETURN,0,0 , 0);
keybd_event(VK_RETURN,0,KEYEVENTF_KEYUP , 0);
just to send the letter 'A' through.

However, it won't bring up the chatbox and nothing happends in the end. If I press T manually in the game to bring it up, the rest executes fine. So, the problem is, using keybd_event function to press 'T' to bring up the chatbox doesn't work. Does it have to do with direct3d 9? Not that I want to go into this option, but could I force the asm function that brings up the chatbox? Would using SendInput() or SendMessage() do anything different for me?

2. Well, you are really suppose to use SendInput (or somthing similar, I forget). keybd_event is depreciated.
Have you checked to see if it even works in a simple program like Notepad or somthing?
And before simulating key presses, you should call SetFocus.

3. None of those methods work with DirectInput/DirectX.

gg

4. Originally Posted by Yarin
Well, you are really suppose to use SendInput (or somthing similar, I forget). keybd_event is depreciated.
Have you checked to see if it even works in a simple program like Notepad or somthing?
And before simulating key presses, you should call SetFocus.
I have gotten it to work. In the first post, I said the only part that doesn't work is bringing up the chatbox with 'T'. It can manually type letters and hit enter just fine. I will try out SendInput and see what happens. Thanks for help.

Originally Posted by Codeplug
None of those methods work with DirectInput/DirectX.

gg
GG? Are you actually trying to insult my intelligence? Well, smarty-pants, what method does work with DirectInput and/or DirectX?

5. First ensure that the target window is in the foreground ie has focus.

Have you registered the 'T' as an Accelerator (key stroke combination used to 'accelerate' use of menus etc). Look at LoadAccelerators() or CreateAcceleratorTable().

Looks like the TranslateAccelerator() function is not catching the 'T' or you are calling TranslateMessage() after it has been recognised as an accelerator. If TranslateAccelerator() returns non zero you should not call TranslateMessage() [as it turns the accelerator into a normal key stroke].