Thread: Simulate Keypress

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    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. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    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. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    None of those methods work with DirectInput/DirectX.

    gg

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by Yarin View Post
    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.

    Quote Originally Posted by Codeplug View Post
    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?

    Google time.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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].
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I simulate a program with a \frac{}{} command
    By xbusterx in forum C++ Programming
    Replies: 21
    Last Post: 09-30-2008, 08:51 PM
  2. Simulate Left Mouse Button (VK_LBUTTON)
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2008, 06:05 PM
  3. Simulating keypress
    By Scarvenger in forum Windows Programming
    Replies: 21
    Last Post: 02-07-2008, 04:38 PM
  4. Programmically simulate clicking on webbrowser2 object
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2007, 01:19 PM
  5. Programmically simulate clicking on webbrowser2 object
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 06-24-2007, 10:34 PM