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

Configure OSX EventsLoop to use charactersIgnoringModifiers in some cases #351

Closed
tmiller opened this issue Nov 19, 2017 · 8 comments
Closed
Labels
C - needs investigation Issue must be confirmed and researched D - average Likely as difficult as most tasks here DS - macos H - help wanted Someone please save us P - low Nice to have S - enhancement Wouldn't this be the coolest?

Comments

@tmiller
Copy link

tmiller commented Nov 19, 2017

I use alacritty as a terminal on OSX, but realized my alt key would not behave correctly. Looking into the issue I ran across this alacritty/alacritty#62. I first modified glutin, then winit to add support using the following patch:

diff --git a/src/platform/macos/events_loop.rs b/src/platform/macos/events_loop.rs
index 8973ab5e..f80446fd 100644
--- a/src/platform/macos/events_loop.rs
+++ b/src/platform/macos/events_loop.rs
@@ -313,7 +313,13 @@ impl EventsLoop {

             appkit::NSKeyDown => {
                 let mut events = std::collections::VecDeque::new();
-                let received_c_str = foundation::NSString::UTF8String(ns_event.characters());
+                let e_mods = event_mods(ns_event);
+
+                let received_c_str = match e_mods.alt {
+                    true => foundation::NSString::UTF8String(ns_event.charactersIgnoringModifiers()),
+                    _ => foundation::NSString::UTF8String(ns_event.characters()),
+                };
+
                 let received_str = std::ffi::CStr::from_ptr(received_c_str);

                 let vkey =  to_virtual_key_code(NSEvent::keyCode(ns_event));
@@ -325,7 +331,7 @@ impl EventsLoop {
                         state: state,
                         scancode: code,
                         virtual_keycode: vkey,
-                        modifiers: event_mods(ns_event),
+                        modifiers: e_mods,
                     },
                 };
                 for received_char in std::str::from_utf8(received_str.to_bytes()).unwrap().chars() {

I'd like to make this behavior configurable in some way, but I don't know the best way to go about modifying winit to configure this behavior.

@jwilm
Copy link
Contributor

jwilm commented Nov 19, 2017

Thanks for figuring out what needed to be done here @tmiller! They way platform specific stuff is typically configured is through one of the os extension traits. For instance, there's already a winit::os::macos::WindowExt. Something similar could be done for the EventsLoop.

By the way, is alt the only modifier to check for in this case? How does this feature behave in other applications having "alt as meta" when additional modifiers are active?

@tmiller
Copy link
Author

tmiller commented Nov 19, 2017

Thanks, I'll look to see how winit::os::macos::WindowExt is done, and see if I can come up with something.

By the way, is alt the only modifier to check for in this case?

Good question, we may need to check to see if alt is the only modifier being activated.

How does this feature behave in other applications having "alt as meta" when additional modifiers are active?

My current approach is naive and suspect this would break when alt is used with other modifiers. I typically don't use other modifiers with alt. I'll see how it behaves with programs like emacs that use multiple modifiers.

@francesca64 francesca64 added S - enhancement Wouldn't this be the coolest? H - help wanted Someone please save us C - needs investigation Issue must be confirmed and researched D - average Likely as difficult as most tasks here P - low Nice to have labels May 6, 2018
@jdek
Copy link

jdek commented Dec 18, 2019

Tried to update this patch for current master but my attempt doesn't seem to do anything, though I don't see how this would be any different?

diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs
index 2b69bfb..0d0b17f 100644
--- a/src/platform_impl/macos/view.rs
+++ b/src/platform_impl/macos/view.rs
@@ -555,7 +555,9 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
         let state_ptr: *mut c_void = *this.get_ivar("winitState");
         let state = &mut *(state_ptr as *mut ViewState);
         let window_id = WindowId(get_window_id(state.ns_window));
-        let characters = get_characters(event, false);
+
+        let e_mods = event_mods(event);
+        let characters = get_characters(event, e_mods.alt);
 
         state.raw_characters = Some(characters.clone());
 
@@ -572,7 +574,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
                     state: ElementState::Pressed,
                     scancode,
                     virtual_keycode,
-                    modifiers: event_mods(event),
+                    modifiers: e_mods,
                 },
                 is_synthetic: false,
             },

@tmiller
Copy link
Author

tmiller commented Dec 19, 2019

At the time get_characters was not available and after looking at the code it seems you are right this is an improvement. However, I'm not sure why it doesn't work. Initially I was working on this to make this setting configurable to Alacritty. I have given up on that and now deal with the issue by configuring the keybindings for Alacritty using this issue comment

@jdek
Copy link

jdek commented Dec 19, 2019

Yeah, I saw that but it's not really a good fix IMO, and should be fixed properly here instead. Some info on what a proper fix should be from someone knowledgeable on the winit project might be helpful.


Edit:

Also using the following in my Alacritty Cargo.toml to actually test this.

[patch."https://github.com/rust-windowing/winit"]
winit = { path = 'winit' }

@frostoov
Copy link

Tried to update this patch for current master but my attempt doesn't seem to do anything, though I don't see how this would be any different?

This patch works:

diff --git a/src/platform_impl/macos/view.rs b/src/platform_impl/macos/view.rs
index 5da5b536..6826605b 100644
--- a/src/platform_impl/macos/view.rs
+++ b/src/platform_impl/macos/view.rs
@@ -646,7 +646,9 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
         let state_ptr: *mut c_void = *this.get_ivar("winitState");
         let state = &mut *(state_ptr as *mut ViewState);
         let window_id = WindowId(get_window_id(state.ns_window));
-        let characters = get_characters(event, false);
+
+        let e_mods = event_mods(event);
+        let characters = get_characters(event, e_mods.alt());
 
         state.raw_characters = Some(characters.clone());
 
@@ -666,7 +668,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
                     state: ElementState::Pressed,
                     scancode,
                     virtual_keycode,
-                    modifiers: event_mods(event),
+                    modifiers: e_mods,
                 },
                 is_synthetic: false,
             },
@@ -675,7 +677,7 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
         let pass_along = {
             AppState::queue_event(EventWrapper::StaticEvent(window_event));
             // Emit `ReceivedCharacter` for key repeats
-            if is_repeat && state.is_key_down {
+            if is_repeat && state.is_key_down || e_mods.alt() {
                 for character in characters.chars().filter(|c| !is_corporate_character(*c)) {
                     AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
                         window_id,

@maroider
Copy link
Member

maroider commented May 27, 2021

There is ongoing work to improve the keyboard input API (#1806), and it may be the case that the changes introduced in #1890 exposes the data you're after here.

@kchibisov
Copy link
Member

This is exposed via the KeyEvent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C - needs investigation Issue must be confirmed and researched D - average Likely as difficult as most tasks here DS - macos H - help wanted Someone please save us P - low Nice to have S - enhancement Wouldn't this be the coolest?
Development

No branches or pull requests

8 participants