Thread: Simulating keyboard event on Windows xp

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    Simulating keyboard event on Windows xp

    Hi all,

    I'm looking for a way to simulate key pressing on Windows XP. I have a program that can recognize my voice, and I want to map the voice command to some keys on the keyboard. For example, when playing games, by saying "map" the program can bring up the in-game map.

    I'm working with VC++ 2005 and I've tried both keybd_event and SendInput functions, but they are just too weak. By weak I mean they work in some games(e.g. World of Warcraft), but fail to work in the others(e.g. Neverwinter Nights, by saying "map" it won't bring up the map like it was in WoW, but by directly pressing the key 'm' it works.).

    I've tried to use SetForegroundWindow() but it doesn't seem to affect anything.

    I use mouse_event() function to simulate the mouse event based on the voice command, and it works fine for pretty much all games that I have tested(without using SetForegroundWindow()). However when I speak a voice command that suppose to simulate a key press it doesn't not work for all games.

    Any idea why?

    The structure of my speech recognition code looks like:
    Code:
    void SRHandler::initEngine(LPCWSTR gramFile)
    {
    //-------initialize Speech Application Programming Interface -----//
    }
    
    void SRHandler::processRecoEvent(HWND hWnd)
    {
    //-------if voice command is recognized ---//
       executeCommand(evt.RecoResult(), hWnd);
      //............
    }
    
    void SRHandler::executeCommand(ISpPhrase *pPhrase, HWND hWnd)
    {
      //call functions such as SendInput to simulate keyboard event or use mouse_event() to simulate mouse event
    }
    I'm wondering if there are other ways of simulating keyboard events?

    Many thanks.

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    Simulate keyboard event on Windows xp

    Hi all,

    I'm looking for a way to simulate key pressing on Windows XP. I have a program that can recognize my voice, and I want to map the voice command to some keys on the keyboard. For example, when playing games, by saying "map" the program can bring up the in-game map.

    I'm working with VC++ 2005 and I've tried both keybd_event and SendInput functions, but they are just too weak. By weak I mean they work in some games(e.g. World of Warcraft), but fail to work in the others(e.g. Neverwinter Nights, by saying "map" it won't bring up the map like it was in WoW, but by directly pressing the key 'm' it works.).

    I've tried to use SetForegroundWindow() but it doesn't seem to affect anything.

    I use mouse_event() function to simulate the mouse event based on the voice command, and it works fine for pretty much all games that I have tested(without using SetForegroundWindow()). However when I speak a voice command that suppose to simulate a key press it doesn't not work for all games.

    Any idea why?

    The structure of my speech recognition code looks like:
    Code:
    void SRHandler::initEngine(LPCWSTR gramFile)
    {
    //-------initialize Speech Application Programming Interface -----//
    }
    
    void SRHandler::processRecoEvent(HWND hWnd)
    {
    //-------if voice command is recognized ---//
       executeCommand(evt.RecoResult(), hWnd);
      //............
    }
    
    void SRHandler::executeCommand(ISpPhrase *pPhrase, HWND hWnd)
    {
      //call functions such as SendInput to simulate keyboard event or use mouse_event() to simulate mouse event
    }
    I'm wondering if there are other ways of simulating keyboard events?

    Many thanks.

  3. #3

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    DirectX / OpenGL use different approach to keyboard/mouse handling.
    Just GET it OFF out my mind!!

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    I answered your question in the windows forum, cross posting will not get your question answered any faster, and will most likely result in either a merge or the thread being deleted.

    As for why some games do not respond, they do this to thwart macro/bot programs. How they do it is by directly polling the keystate information.

    http://msdn.microsoft.com/en-us/libr...04(VS.85).aspx

    Quote Originally Posted by MSDN
    The keyboard driver's interrupt handler calls the keybd_event function.
    Which means if that isn't working, either you are sending the wrong vKey values, or there is a problem with your program and or voice recognition software.
    Last edited by abachler; 08-17-2009 at 04:21 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved as per abachler's note about cross posting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows XP (and Vista) shiney look and feel
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-18-2005, 02:56 PM
  2. hbrBackground (WNDCLASSEX) problems in Windows XP
    By codec in forum Windows Programming
    Replies: 4
    Last Post: 05-17-2004, 08:30 AM
  3. Application Termination Problem in Windows XP
    By wasabee in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:53 PM
  4. I just got Window's XP (Home Edition)
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 11-03-2001, 09:40 PM
  5. windows xp and aol (im's)
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-26-2001, 06:50 PM