I have written an On Screen Keyboard using firemonkey.
I simulate key events with this code:
void kbdEvent(unsigned int key, int modifiers)
{
::CGEventSourceRef source =
CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
::CGEventRef keyDown = CGEventCreateKeyboardEvent(source,
(::CGKeyCode)key, true);
::CGEventRef keyUp = CGEventCreateKeyboardEvent(source, (::CGKeyCode)key,
false);
CGEventSetFlags(keyDown, modifiers);
CGEventPost(kCGHIDEventTap, keyDown);
CGEventPost(kCGHIDEventTap, keyUp);
CFRelease(keyUp);
CFRelease(keyDown);
CFRelease(source);
}
The problem is that when I press a key then my form receives focus and the generated event is not received by target app. If I put the following code in a timer there is no problem and the 'a' will typed in any editable target:
kbdEvent(kVK_ANSI_A, 0);
How can I do it without receive focus?