Skip to content

Commit

Permalink
[macOS] correct sendkey.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyenvm committed Nov 6, 2019
1 parent 6377fe4 commit 590bf93
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions Sources/OpenKey/macOS/ModernKey/OpenKey.mm
Expand Up @@ -206,9 +206,13 @@ void InsertKeyLength(const Uint8& len) {

void SendPureCharacter(const Uint16& ch) {
_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, 1, &ch);
CGEventKeyboardSetUnicodeString(_newEventUp, 1, &ch);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
CFRelease(_newEventDown);
CFRelease(_newEventUp);
if (IS_DOUBLE_CODE(vCodeTable)) {
InsertKeyLength(1);
}
Expand All @@ -235,26 +239,35 @@ void SendKeyCode(Uint32 data) {
CGEventSetFlags(_newEventUp, _privateFlag);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
CFRelease(_newEventUp);
} else {
if (vCodeTable == 0) { //unicode 2 bytes code
_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, 1, &_newChar);
CGEventKeyboardSetUnicodeString(_newEventUp, 1, &_newChar);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
} else if (vCodeTable == 1 || vCodeTable == 2 || vCodeTable == 4) { //others such as VNI Windows, TCVN3: 1 byte code
_newCharHi = HIBYTE(_newChar);
_newChar = LOBYTE(_newChar);

_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, 1, &_newChar);
CGEventKeyboardSetUnicodeString(_newEventUp, 1, &_newChar);
CGEventTapPostEvent(_proxy, _newEventDown);

CGEventTapPostEvent(_proxy, _newEventUp);
if (_newCharHi > 32) {
if (vCodeTable == 2) //VNI
InsertKeyLength(2);
CFRelease(_newEventDown);
CFRelease(_newEventUp);
_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, 1, &_newCharHi);
CGEventKeyboardSetUnicodeString(_newEventUp, 1, &_newCharHi);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
} else {
if (vCodeTable == 2) //VNI
InsertKeyLength(1);
Expand All @@ -266,11 +279,15 @@ void SendKeyCode(Uint32 data) {
_uniChar[1] = _newCharHi > 0 ? (_unicodeCompoundMark[_newCharHi - 1]) : 0;
InsertKeyLength(_newCharHi > 0 ? 2 : 1);
_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, (_newCharHi > 0 ? 2 : 1), _uniChar);
CGEventKeyboardSetUnicodeString(_newEventUp, (_newCharHi > 0 ? 2 : 1), _uniChar);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
}
}
CFRelease(_newEventDown);
CFRelease(_newEventUp);
}

void SendEmptyCharacter() {
Expand All @@ -283,9 +300,13 @@ void SendEmptyCharacter() {
}

_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, 1, &_newChar);
CGEventKeyboardSetUnicodeString(_newEventUp, 1, &_newChar);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
CFRelease(_newEventDown);
CFRelease(_newEventUp);
}

void SendVirtualKey(const Byte& vKey) {
Expand Down Expand Up @@ -427,9 +448,13 @@ void SendNewCharString(const bool& dataFromMacro=false, const Uint16& offset=0)
}

_newEventDown = CGEventCreateKeyboardEvent(myEventSource, 0, true);
_newEventUp = CGEventCreateKeyboardEvent(myEventSource, 0, false);
CGEventKeyboardSetUnicodeString(_newEventDown, _willContinuteSending ? 16 : _newCharSize - offset, _newCharString);
CGEventKeyboardSetUnicodeString(_newEventUp, _willContinuteSending ? 16 : _newCharSize - offset, _newCharString);
CGEventTapPostEvent(_proxy, _newEventDown);
CGEventTapPostEvent(_proxy, _newEventUp);
CFRelease(_newEventDown);
CFRelease(_newEventUp);

if (_willContinuteSending) {
SendNewCharString(dataFromMacro, dataFromMacro ? _k : 16);
Expand Down

0 comments on commit 590bf93

Please sign in to comment.