Skip to content

Commit ea45808

Browse files
committed
Encode to utf-16 for CGEventKeyboardSetUnicodeString
1 parent daadbcc commit ea45808

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/keypress.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void tapKey(char c, MMKeyFlags flags)
191191
}
192192

193193
#if defined(IS_MACOSX)
194-
void toggleUnicodeKey(UniChar ch, const bool down)
194+
void toggleUnicodeKey(unsigned long ch, const bool down)
195195
{
196196
/* This function relies on the convenient
197197
* CGEventKeyboardSetUnicodeString(), which allows us to not have to
@@ -204,16 +204,25 @@ void toggleUnicodeKey(UniChar ch, const bool down)
204204
return;
205205
}
206206

207-
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
207+
if (ch > 0xFFFF) {
208+
// encode to utf-16 if necessary
209+
unsigned short surrogates[] = {
210+
0xD800 + ((ch - 0x10000) >> 10),
211+
0xDC00 + (ch & 0x3FF)
212+
};
213+
214+
CGEventKeyboardSetUnicodeString(keyEvent, 2, &surrogates);
215+
} else {
216+
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
217+
}
208218

209219
CGEventPost(kCGSessionEventTap, keyEvent);
210220
CFRelease(keyEvent);
211221
}
212222

213223
void toggleUniKey(char c, const bool down)
214224
{
215-
UniChar ch = (UniChar)c; /* Convert to unsigned char */
216-
toggleUnicodeKey(ch, down);
225+
toggleUnicodeKey(c, down);
217226
}
218227
#else
219228
#define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE)
@@ -227,10 +236,10 @@ static void tapUniKey(char c)
227236

228237
void typeString(const char *str)
229238
{
230-
unsigned long c;
231-
unsigned long c1;
232-
unsigned long c2;
233-
unsigned long c3;
239+
unsigned short c;
240+
unsigned short c1;
241+
unsigned short c2;
242+
unsigned short c3;
234243
unsigned long n;
235244

236245
while (*str != '\0') {

0 commit comments

Comments
 (0)