Simulating keyboard event on Windows xp

This is a discussion on Simulating keyboard event on Windows xp within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I'm looking for a way to simulate key pressing on Windows XP. I have a program that can ...

  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
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Moved as per abachler's note about cross posting.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21