Results 1 to 9 of 9

Thread: Simulate keyboard

  1. #1
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default Simulate keyboard

    Is there a way to simulate a keystoke through C++?

  2. #2
    Join Date
    Sep 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Not in standard C++. Which platform are you using?

  3. #3
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    Dev-C++

  4. #4
    Join Date
    Sep 2009
    Posts
    66
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I meant operating system, but that tells enough.

    First functions that pop to my mind are SendInput and SendMessage. Check MSDN for info.

    You should throw Dev-C++ away. Now. It hasn't been updated in years, last commit was made in 20.8.2005. Get something which is more up to date.

  5. #5
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Depends if you use Linux, Window$ or Mac.

    On Windows, try SendMessage/SendInput.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  6. #6
    Join Date
    Apr 2007
    Posts
    3,152
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    is there a crosser platform way?
    SCAR Tutorials: The Form Tutorial | Types, Arrays, and Classes
    Programming Projects: NotePad | Tetris | Chess


  7. #7
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Dan Cardin View Post
    is there a crosser platform way?
    No. Not unless you write one yourself.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  8. #8
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Dan Cardin View Post
    is there a crosser platform way?
    I can't remember, but I believe I put it into scar++ (I may not have put it in yet).

    There is no universal way to send keys. It is very much OS dependent.

  9. #9
    Join Date
    Nov 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Here's some functions that supports OS X (this isn't C++, it's just C, but it should work just the same):

    Code:
    #if defined(__APPLE__) && defined(__MACH__)
    #define MACOSX 1
    #else
    #error "I said this only supports OS X!"
    #endif
    
    #include <assert.h> // For assert()
    #include <ApplicationServices/ApplicationServices.h> // OSX-only header
    
    void toggleKey(UniChar c, const bool down)
    {
    	CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
    	assert(keyEvent != NULL);
    
    	CGEventSetFlags(keyEvent, 0);
    	CGEventKeyboardSetUnicodeString(keyEvent, 1, &c);
    
    	CGEventPost(kCGSessionEventTap, keyEvent);
    	CFRelease(keyEvent);
    }
    
    inline void tapKey(UniChar c)
    {
    	toggleKey(c, true);
    	toggleKey(c, false);
    }
    
    inline void typeString(CFStringRef str)
    {
    	unsigned i, len = CFStringGetLength(str);
    	for (i = 0; i < len; ++i) {
    		tapKey(CFStringGetCharacterAtIndex(str, i));
    	}
    }
    For other platforms, take a look at the "SendKeys" function under Keyboard.cpp in Scar++.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •