JPHamlett
2009-11-08 13:42:36
Is there a way to simulate a keystoke through C++?
2009-11-08 13:42:36
2009-11-08 14:35:21
2009-11-08 14:37:56
2009-11-08 17:15:29
2009-11-08 17:17:34
2009-11-08 17:45:53
2009-11-08 18:25:56
From: Dan Cardin
is there a crosser platform way?
2009-11-10 13:42:25
From: Dan Cardin
is there a crosser platform way?
2009-11-12 13:47:09
#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));
}
}