Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
EscKeyWidget: now supports cmd-opt-esc
Browse files Browse the repository at this point in the history
  • Loading branch information
billziss-gh committed Aug 29, 2018
1 parent b26137d commit 4890e35
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
46 changes: 27 additions & 19 deletions src/System/KeyEvent.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,9 @@
*/

#include "KeyEvent.h"
#include <CoreGraphics/CoreGraphics.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <pthread.h>

static bool PostKeyEvent(uint16_t keyCode, bool keyDown)
{
CGEventRef event = CGEventCreateKeyboardEvent(0, keyCode, keyDown);
if (0 != event)
{
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
return true;
}
return false;
}

void PostKeyPress(uint16_t keyCode)
{
PostKeyEvent(keyCode, true);
PostKeyEvent(keyCode, false);
}

static pthread_once_t hid_conn_once = PTHREAD_ONCE_INIT;
static io_connect_t hid_conn = 0;

Expand Down Expand Up @@ -60,6 +42,32 @@ static void hid_conn_initonce(void)
IOObjectRelease(serv);
}

void PostKeyPress(uint16_t keyCode, uint32_t flags)
{
NXEventData event = { 0 };
IOGPoint point = { 0 };
kern_return_t ret;

pthread_once(&hid_conn_once, hid_conn_initonce);
if (0 == hid_conn)
return;

event.key.repeat = 0;
event.key.keyCode = keyCode;
event.key.charSet = NX_ASCIISET;
event.key.charCode = 0;
event.key.origCharSet = event.key.charSet;
event.key.origCharCode = event.key.charCode;

ret = IOHIDPostEvent(hid_conn, NX_KEYDOWN, point, &event, kNXEventDataVersion, flags, 0);
if (KERN_SUCCESS != ret)
return;

ret = IOHIDPostEvent(hid_conn, NX_KEYUP, point, &event, kNXEventDataVersion, flags, 0);
if (KERN_SUCCESS != ret)
return;
}

void PostAuxKeyPress(uint16_t auxKeyCode)
{
NXEventData event = { 0 };
Expand Down
2 changes: 1 addition & 1 deletion src/System/KeyEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <stdint.h>

void PostKeyPress(uint16_t keyCode);
void PostKeyPress(uint16_t keyCode, uint32_t flags);
void PostAuxKeyPress(uint16_t auxKeyCode);

#endif
2 changes: 1 addition & 1 deletion src/Widgets/EscKeyWidget.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ - (void)dealloc

- (void)click:(id)sender
{
PostKeyPress(0x35/*kVK_Escape*/);
PostKeyPress(0x35/*kVK_Escape*/, [NSEvent modifierFlags]);
}
@end

0 comments on commit 4890e35

Please sign in to comment.