Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix Ctrl-key chords on MacOS with the new input. #1599

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/macosx/keybd.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY* dpy)
if (pressed) {
int32_t unichar = 0;
bool new_input = _al_get_keyboard_compat_version() >= AL_ID(5, 2, 10, 0);
NSString *raw_characters = [event charactersIgnoringModifiers];
NSString *characters = [event characters];
UniChar raw_character = ([raw_characters length] > 0) ? [raw_characters characterAtIndex: 0] : 0;
UniChar character = ([characters length] > 0) ? [characters characterAtIndex: 0] : 0;

if (new_input) {
Expand All @@ -298,18 +296,30 @@ void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY* dpy)
4,
&unicode_length,
unicode_string);

if (unicode_length > 0) {
ALLEGRO_USTR *ustr = al_ustr_new_from_utf16(unicode_string);
unichar = al_ustr_get(ustr, 0);
/* TODO: Possibly add an option to emit multiple events here.
* At the moment, we take the last key, because when an invalid
* dead key combination is entered, unicode string contains a character
* representaiton of the dead key + the last pressed key.
* We opt to return the last pressed key.
*/
unichar = al_ustr_get(ustr, al_ustr_offset(ustr, (int)unicode_length - 1));
if (unichar < 0)
unichar = 0;
/* For some reason, pad enter sends a ^C. */
if (scancode == ALLEGRO_KEY_PAD_ENTER && unichar == 3)
else if (scancode == ALLEGRO_KEY_PAD_ENTER && unichar == 3)
unichar = '\r';
/* Single out the few printable characters under 32 */
if (unichar < ' ' && (unichar != '\r' && unichar != '\t' && unichar != '\b'))
unichar = 0;
/* This is here to override the Ctrl/Cmd fixes for backspace. */
else if (scancode == ALLEGRO_KEY_BACKSPACE)
unichar = '\b';
/* For some reason, Ctrl-<key> sends capital version of the character,
and not the correct invisible character. */
else if (key_shifts & ALLEGRO_KEYMOD_CTRL)
unichar = character;
/* For some reason, Cmd-<key> converts characters to Ctrl-<key>. */
else if (key_shifts & ALLEGRO_KEYMOD_COMMAND)
unichar = character;
al_ustr_free(ustr);
}
CFRelease(keyboard_input);
Expand All @@ -318,16 +328,16 @@ void _al_osx_keyboard_handler(int pressed, NSEvent *event, ALLEGRO_DISPLAY* dpy)
unichar = character;

/* Apple maps function, arrow, and other keys to Unicode points.
https://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/CORPCHAR.TXT
We want to generate CHAR events for them, so we'll override the translation logic.
_handle_key_press will set the unichar back to 0 for these keys. */
if (character >= 0xF700 && character <= 0xF747)
unichar = -1;
/* The delete key. */
if (character == 0xF728 && new_input)
unichar = 127;
/* Special processing to send character 1 for CTRL-A, 2 for CTRL-B etc. */
if ((key_shifts & ALLEGRO_KEYMOD_CTRL) && (isalpha(raw_character)))
unichar = tolower(raw_character) - 'a' + 1;
if (character >= 0xF700 && character <= 0xF747) {
/* The old input did not handle this key (delete) correctly. We preserve the old behavior. */
if (new_input && character == 0xF728)
unichar = 127;
else
unichar = -1;
}
bool is_repeat = pressed ? ([event isARepeat] == YES) : false;
_handle_key_press(dpy, unichar, scancode, key_shifts, is_repeat);
}
Expand Down
Loading