Skip to content

Provide a way to handle VirtualKeyCode or Utf8 string #115

Closed

Description

@jwilm

Alacritty is currently using a fork of glutin which includes the decoded UTF-8 String with the glutin::Event::KeyboardInput. This was added to support either handling the VirtualKeyCode and active modifiers as a key binding or just printing the decoded UTF-8. For example:

glutin::Event::KeyboardInput(state, _code, key, mods, string) => {
    if let Some(key) = key { // VirtualKeyCode
        if self.process_key_bindings(mods, key) {
            return;
        }
    }

    self.print(string);
},

Without including the String in this event, it was very difficult to have this logic where we check for keybindings or else fall back to printing. The string was delivered in potentially several Event::ReceivedChar events and followed by the KeyboardInput event. There is currently no way to know which ReceivedChar events go with which KeyboardInput.

I realize passing a string with this event may not be ideal since String allocates, and I am open to other solutions. Even so, events are already buffered on the heap, and it seems like adding an allocation for the string versus a series of ReceivedChar events is a reasonable trade.

A couple of questions:

  1. What do you think about supporting this use case in winit/glutin?
  2. What are your thoughts on including a String in the KeyboardInput event?

Activity

  1. added
    S - api
    S - enhancement
    on Jan 20, 2017
  2. tomaka commented on Jan 20, 2017

    @tomaka
    Contributor

    What are your thoughts on including a String in the KeyboardInput event?

    I don't have any thoughts. Winit aims to be the lowest common denominator of all windowing systems.
    If windowing systems provide a way to associate characters with the keyboard input, then winit should provide a way too.

  3. jwilm commented on Jan 20, 2017

    @jwilm
    ContributorAuthor

    If windowing systems provide a way to associate characters with the keyboard input, then winit should provide a way too.

    winit already provides this information through winit::Event::ReceivedCharacter. The problem is that with the events iterator, there's no way to know which ReceivedCharacter events are associated with some KeyboardInput event. This issue suggests to address that by including a String with the KeyboardInput instead of a series of ReceivedCharacter.

  4. rigtorp commented on Jan 21, 2017

    @rigtorp
    Contributor

    I seems pretty silly to worry about heap allocations that would occur at a rate less then 10 times per second.

    GLFW has a similar ReceivedCharacter event and I suspect it is based on the WM_CHAR win32 event: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646276(v=vs.85).aspx

    Maybe there are issues implementing this unified keyboard event on windows with non-ascii input?

  5. tomaka commented on Jan 21, 2017

    @tomaka
    Contributor

    winit already provides this information through winit::Event::ReceivedCharacter. The problem is that with the events iterator, there's no way to know which ReceivedCharacter events are associated with some KeyboardInput event.

    I'm saying that if Windows, X11, Wayland and Cocoa all provide a way to associate a keyboard input with a character, then we should go ahead and expose this in winit.

  6. added a commit that references this issue on Mar 29, 2017
  7. jwilm commented on May 8, 2017

    @jwilm
    ContributorAuthor

    Seems like there's nothing to be done here since win32 can't support this. Thanks for the info @rigtorp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    S - apiS - enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions