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

textadept-qt: strange behaviour in keypress events #301

Closed
paaguti opened this issue Dec 4, 2022 · 13 comments
Closed

textadept-qt: strange behaviour in keypress events #301

paaguti opened this issue Dec 4, 2022 · 13 comments

Comments

@paaguti
Copy link

paaguti commented Dec 4, 2022

This happens with a freshly compiled textadept using Qt on Linux.

I add the following key handler in .textadept/init.lua

events.connect(events.KEYPRESS, function(code, shift, control, alt, cmd, caps)
   print(code, shift, control, alt, cmd, caps)
end)

I repeat the key sequence 'a', shift+'a':

Without caps lock, it prints:

65	false	false	false	false	nil
65	true	false	false	false	nil

With caps lock:

65	false	false	false	false	nil
65	true	false	false	false	nil

Apparently, the caps doesn't seem to be set at all

@orbitalquark
Copy link
Owner

orbitalquark commented Dec 4, 2022 via email

@paaguti
Copy link
Author

paaguti commented Dec 4, 2022 via email

@mhwombat
Copy link

mhwombat commented Dec 5, 2022

So I understand that Qt cannot detect if caps lock is on or off. But surely there is some way to distinguish between the inputs 'a' and 'A'?

For me personally, I can live without uppercase key bindings. But as the author of textadept-hydra, I'd like to make this work if it's possible.

@orbitalquark
Copy link
Owner

orbitalquark commented Dec 5, 2022 via email

@paaguti
Copy link
Author

paaguti commented Dec 6, 2022

@mhwombat: I have come up with the following work-aroud:

      -- Qt has no caps-lock :(
      key = code >= 32 and code < 256 and string.char(code) or keys.KEYSYMS[code]
      if not key then
         return
      end
      -- Translate code -- shift not pressed means lowercase
      if code >= 65 and code <= 90 then
         key = string.char(code + (shift and 0 or 32))
         shift=false
      end

<hmm>but that ignores the caps-lock </hmm>

@paaguti
Copy link
Author

paaguti commented Dec 13, 2022

@orbitalquark: Maybe if we had a standard way of translating key pressed to beautified strings à la 'ctrl+A' in textadept, many modules like hydra might benefit...

Just an idea... so I isolated the code in core/keys.lua and provided a function called key_sequence(). This makes the hydra module code much cleaner:

events.connect(events.KEYPRESS, function(code, shift, control, alt, cmd, caps)
  --
  -- If textadept provided a common key_sequence() translation:
  --
  local key_seq = keys.key_sequence(code, shift, control, alt, cmd, caps)
  if not key_seq then return end
  -- print(code, keys.KEYSYMS[code], shift, control, alt, cmd, caps)
  -- ui.statusbar_text = key_seq
  return handle_key_seq(key_seq)
end, 1)

Isn't that clean ;-) ?

@mhwombat
Copy link

I was thinking along the same lines as @paaguti; it would be best if textadept provided a way to determine what key was pressed that is independent of whether it was built with GTK or QT or whatever.

@mhwombat
Copy link

Even apart from the QT/GTK differences, I was thinking that it would be very beneficial to factor the keypress interpretation logic out of events.connect into a separate function, and provide that in the API. Otherwise every module that handles keyboard events has to replicate it.

@orbitalquark
Copy link
Owner

orbitalquark commented Dec 24, 2022 via email

@paaguti
Copy link
Author

paaguti commented Dec 26, 2022

IMvHO, the first is the least intrusive and add no migration woes (we have enough ;-) )

@orbitalquark
Copy link
Owner

orbitalquark commented Dec 26, 2022 via email

@orbitalquark
Copy link
Owner

I have changed events.KEYPRESS to emit only the string representation of the key pressed, e.g. ctrl+a, ctrl+A, ctrl+alt+a, etc.: 0abbc29.

This change simplified Textadept's internal key handling code in many places, and greatly simplified emitting keys in the unit tests, so I felt it was a worthwhile change.

I am sorry in advance for any inconvenience this change causes. Let me know if I can be of any assistance in migrating old event handlers.

@paaguti
Copy link
Author

paaguti commented Dec 30, 2022 via email

@paaguti paaguti closed this as completed Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants