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

The kitty shell default shortcut does not work under macOS #4109

Closed
page-down opened this issue Oct 10, 2021 · 11 comments
Closed

The kitty shell default shortcut does not work under macOS #4109

page-down opened this issue Oct 10, 2021 · 11 comments
Labels

Comments

@page-down
Copy link
Contributor

Describe the bug
The default shortcut for kitty shell is kitty_mod+escape (Ctrl+Shift+ESC).
However, nothing happens when you press this key combination under macOS. It works fine under Linux.

I wonder if there is a special reason why the bound action is not executed.

To Reproduce
Steps to reproduce the behavior:

  1. Press Ctrl+Shift+ESC under macOS
  2. No kitty shell window.

Screenshots
n/a

Environment details

  • macOS 11.6
  • kitty from master branch

Additional context

kitty --config=NONE -o "map ctrl+shift+escape kitty_shell window" --debug-input
...
# ESC
on_key_input: glfw key: 0xe000 native_code: 0x35 action: PRESS mods: none text: '' state: 1 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
updated pre-edit text: ''
Press: native_key: 0x35 (<cc>) glfw_key: 0xe000 mods: none char_count: 1 deadKeyState: 0 repeat: 0 text: <none> glfw_key: ESCAPE marked_text: 
on_key_input: glfw key: 0xe000 native_code: 0x35 action: REPEAT mods: none text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
sent encoded key to child
Release: native_key: 0x35 (<cc>) glfw_key: 0xe000 mods: none 
on_key_input: glfw key: 0xe000 native_code: 0x35 action: RELEASE mods: none text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
ignoring as keyboard mode does not support encoding this event

# Ctrl+Shift+ESC
on_key_input: glfw key: 0xe062 native_code: 0x3b action: PRESS mods: ctrl text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
ignoring as keyboard mode does not support encoding this event
on_key_input: glfw key: 0xe061 native_code: 0x38 action: PRESS mods: ctrl+shift text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
ignoring as keyboard mode does not support encoding this event
Release: native_key: 0x35 (<cc>) glfw_key: 0xe000 mods: ctrl+shift 
on_key_input: glfw key: 0xe062 native_code: 0x3b action: RELEASE mods: shift text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
ignoring as keyboard mode does not support encoding this event
on_key_input: glfw key: 0xe061 native_code: 0x38 action: RELEASE mods: none text: '' state: 0 updateIMEState: 189.000000, 6.000000, 7.000000, 13.000000
ignoring as keyboard mode does not support encoding this event

It seems that any modifier key with the ESC key suffers from this issue.

@page-down page-down added the bug label Oct 10, 2021
@kovidgoyal
Copy link
Owner

This will almost certainly be because cocoa is intercepting
esc+modifier. From the debug log, it is clear that the esc keypress is
not reaching kitty. I'm nt a macOS user so I'm not sure, but is there
some cocoa global shortcut with esc+ctrl ??

You can also add a print around line 514 in cocoa_init.m to see if the
keypress is reaching at least till there.

@page-down
Copy link
Contributor Author

but is there some cocoa global shortcut with esc+ctrl ?

I don't recall any default cocoa OS window shortcuts related to Ctrl and ESC, except for the system-wide Cmd+Opt+ESC to force quit applications.

It is not globally intercepted by the OS or other software.

You can also add a print around line 514 in cocoa_init.m to see if the keypress is reaching at least till there.

Event with ESC keycode reaches keydown_block.

Thanks, confirmed that the following patch solves the ESC related shortcut issue under macOS, keyup responds normally so no changes are made.

diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m
index a43aa24e..b9a7fbc0 100644
--- a/glfw/cocoa_init.m
+++ b/glfw/cocoa_init.m
@@ -471,6 +471,19 @@ - (void)render_frame_received:(id)displayIDAsID
     return false;
 }
 
+
+static bool
+is_modifiers_esc(NSEvent *event, NSEventModifierFlags modifierFlags) {
+    if (event.keyCode != kVK_Escape) return false;
+    if (
+            (modifierFlags & NSEventModifierFlagControl) != 0 ||
+            (modifierFlags & NSEventModifierFlagShift) != 0 ||
+            (modifierFlags & NSEventModifierFlagOption) != 0 ||
+            (modifierFlags & NSEventModifierFlagCommand) != 0
+       ) return true;
+    return false;
+}
+
 GLFWAPI GLFWapplicationshouldhandlereopenfun glfwSetApplicationShouldHandleReopen(GLFWapplicationshouldhandlereopenfun callback) {
     GLFWapplicationshouldhandlereopenfun previous = handle_reopen_callback;
     handle_reopen_callback = callback;
@@ -511,7 +524,7 @@ int _glfwPlatformInit(void)
     NSEvent* (^keydown_block)(NSEvent*) = ^ NSEvent* (NSEvent* event)
     {
         NSEventModifierFlags modifierFlags = [event modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask;
-        if (is_ctrl_tab(event, modifierFlags) || is_cmd_period(event, modifierFlags)) {
+        if (is_ctrl_tab(event, modifierFlags) || is_cmd_period(event, modifierFlags) || is_modifiers_esc(event, modifierFlags)) {
             // Cocoa swallows Ctrl+Tab to cycle between views
             [[NSApp keyWindow].contentView keyDown:event];
         }

The modifiers and ESC key events can be seen as normal with kitty +kitten show_key -m kitty .

@kovidgoyal
Copy link
Owner

In my testing it is only ctrl or cmd that cause problems, not option or
shift. And the problem happens for function keys as well as esc. So I
have pushed a fix for these. Please test. I am somewhat worried that
this will break the use of these keys in some keyboard layouts, we will
have to see.

@page-down
Copy link
Contributor Author

Yes, there is no need to check the option and shift keys.

I have tested the following keys are problematic.

  • Ctrl [+ Shift] [+ Option] + ESC
  • Cmd [+ Shift] [+ Option] + ESC

The latest changes have made the above key combinations work properly.

I don't think it's necessary to deal with function keys, as they are taken by macOS by default, and this should be turned off by the user in the system preferences. The latest commit causes the default function key global shortcuts for macOS are not working properly.

macOS_default_shortcuts

Since I don't have those default system shortcuts enabled, the function keys were working properly before this commit, at least for me.

What worries me most is that I can't find any official macOS dev docs on why Ctrl Cmd with ESC is being intercepted. Hopefully, this will not cause other problems.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Oct 10, 2021 via email

@page-down
Copy link
Contributor Author

What doe these keys do and where in preferences can I find them?

System Preferences -> Keyboard -> Shortcuts

open /System/Library/PreferencePanes/Keyboard.prefPane

Uncheck all unused features and remove all unneeded bindings.

On my system ctrl+shift+f6 which is the shortcut for display the kitty config doesnt work without this change.

With Accessibility Keyboard enabled, ctrl+shift+f6 is used to switch the panel (Accessibility Panel).

Mac accessibility shortcuts - Apple Support

However, it doesn't seem to work on my latest OS. (so Ctrl+Shift+F6 is free to use.) It is possible that the component has been upgraded and the functionality has changed.

Also, if you have pressed the macOS default function shortcut that switches the focus, you need to click the mouse in the window, regain the focus, and press the keys again to get the response. In my tests, I also found that pressing the function shortcut key one after another did not work, and I found that the focus was cut off, and it was hard to notice.


The issue caused by the current changes can be reproduced by these steps.

  • Enable "Move focus to the menu bar (Ctrl+F2)" on macOS System Preferences.
  • Press Ctrl+F2
  • Cannot focus on menu bar.

@kovidgoyal
Copy link
Owner

kovidgoyal commented Oct 11, 2021 via email

@page-down
Copy link
Contributor Author

What shortcut is that? As far as I know cmd+tab is the macOS shortcut to switch focus between applications and cmd+` to switch focus between windows of an application. Both work in kitty.

System Preferences -> Keyboard -> Shortcuts -> Keyboard

  • Ctrl+F1 (macOS Default. Can be changed. Enables the default Ctrl-F2 ~ F6 function keys when pressed.)
  • Ctrl+F2 (Switch focus to the menu bar.)
  • Ctrl+F3 (Switch focus to the Dock.)
  • ...

Since the latest commit no longer handle Ctrl + function keys, the default shortcuts are no longer affected. Confirm that everything above works.

... Press Ctrl+F2 ... Cannot focus on menu bar.

I have fixed this by only overriding ctrl+shift+function key ... is a bit too much work, at least for me ...

Thank you for the time you invested in solving this issue. I've had enough of Apple things for now, as well.
I think the current solution is good enough for normal users (Leave macOS and kitty settings unchanged) or heavy keyboard users (Turn off all default keys for macOS and define various shortcuts in kitty) .

One last problem.

// ctrl+esc and cmd+esc
if (ch == 0x1b && (modifierFlags == NSEventModifierFlagCommand || modifierFlags == NSEventModifierFlagControl)) return true;

The following key combination was not captured as a result. On the previous commit these keys worked properly.

  • Ctrl + Option + ESC
  • Ctrl + Option + Cmd + ESC
  • Ctrl + Shift + Option + ESC
  • Ctrl + Cmd + ESC
  • Ctrl + Shift + Cmd + ESC

It is worth noting that the following keys do not need to be handled. Maybe it can't be captured either.

  • Cmd + Option + ESC (Displays the OS Force Quit Application dialog.)
  • Cmd + Shift + Option + ESC (Force quit the application without confirmation.)

The following are not available and does not matter.

  • Ctrl + Shift + Option + Cmd + ESC

Result of previous commit 2b9408c with kitty +kitten show_key -m kitty .

# Ctrl + Option + ESC
Alt+Ctrl+ESCAPE REPEAT 
CSI 27 ; 7 : 2 u
Alt+Ctrl+ESCAPE RELEASE 
CSI 27 ; 7 : 3 u

# Ctrl + Option + Cmd + ESC
Alt+Ctrl+Super+ESCAPE PRESS 
CSI 27 ; 15 u
Alt+Ctrl+Super+ESCAPE RELEASE 
CSI 27 ; 15 : 3 u

# Ctrl + Shift + Option + ESC
Shift+Alt+Ctrl+ESCAPE PRESS 
CSI 27 ; 8 u
Shift+Alt+Ctrl+ESCAPE RELEASE 
CSI 27 ; 8 : 3 u

# Ctrl + Cmd + ESC
Ctrl+Super+ESCAPE PRESS 
CSI 27 ; 13 u
Ctrl+Super+ESCAPE RELEASE 
CSI 27 ; 13 : 3 u

# Ctrl + Shift + Cmd + ESC
Shift+Ctrl+Super+ESCAPE PRESS 
CSI 27 ; 14 u
Shift+Ctrl+Super+ESCAPE RELEASE 
CSI 27 ; 14 : 3 u

@page-down
Copy link
Contributor Author

In addition to the problem with the ESC shortcut mentioned above, I also found another problem with key events being sent repeatedly.

Please note that the previous message mentioned: with previous commit 2b9408c, Ctrl+Option+ESC REPEAT

# Ctrl + Option + ESC
Alt+Ctrl+ESCAPE REPEAT 
CSI 27 ; 7 : 2 u

Here is the last change dc11b76, after executing the kitty --debug-input kitty +kitten show_key -m kitty command, the first time the ctrl+shift + function shortcut is pressed the REPEAT action appears, is this caused by the key event being sent repeatedly?

Take Ctrl+Shift+F1 as an example.

Press any keys - Ctrl+C or Ctrl+D will terminate
Ctrl+LEFT_CONTROL PRESS 
CSI 57442 ; 5 u

Shift+Ctrl+LEFT_SHIFT PRESS 
CSI 57441 ; 6 u

Shift+Ctrl+F1 REPEAT 
CSI 1 ; 6 : 2 P

Shift+Ctrl+F1 RELEASE 
CSI 1 ; 6 : 3 P

Ctrl+LEFT_SHIFT RELEASE 
CSI 57441 ; 5 : 3 u

LEFT_CONTROL RELEASE 
CSI 57442 ; 1 : 3 u

Subsequent presses of shortcuts seem to be normal.

Shift+Ctrl+F1 PRESS 
CSI 1 ; 6 P
Shift+Ctrl+F1 RELEASE 
CSI 1 ; 6 : 3 P

Is this an acceptable result?

@kovidgoyal
Copy link
Owner

Both issues should now be fixed in master.

@page-down
Copy link
Contributor Author

Thanks, I tested it and so far everything works fine on my system with the keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants