JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.9k stars 1.16k forks source link

Menu. Support arbitrary key shortcuts #914

Open igordmn opened 3 years ago

igordmn commented 3 years ago

Here we implemented shortcuts for Window Menu.

Currently we support only mainstream shortcuts [Ctrl]+[Shift]+[Alt]+[Meta]+Key

Investigate how we can support arbitrary shortcuts that works even if we press some key outside of the our application:

  1. Key1 + Key2 (order doesn't matter)
  2. Key1 + Key2 (order is matter)
  3. LeftCtrl + Key1
  4. Multiple shortcuts for the same action (i.e. LeftCtrl + Key1 and RightCtrl + Key1 or Ctrl-Shift-Z and Ctrl-Y for Redo).

To do that, we need to:

  1. Make a public API for KeyShortcut. One of the possible solutions:
    
    class KeyShortcut(val sets: List<KeySet>)

class KeySet(private val keys: Set) : Set

Usage:

KeyShortcut(Key.CtrlLeft + Key.C, Key.CtrlRight + Key.C)



2.
a. Observe all keys at the global application level, even if application is minimized (there can be a security issue though)

Swing doesn't allow it (`Toolkit.getDefaultToolkit().addAWTEventListener` only works if application in focus)
There is a native library https://github.com/kwhat/jnativehook. We can implement something similar in skiko.

b (alternative). instead of observing keys at the global level we can ask OS to get the current state of the Key.

P.2 is needed because users can press part of the shortcut outside of the application window (Ctrl, for example)
Ifropc commented 2 years ago

Hi, are there any updates on this issue? Is my understanding correct that the only way to create a global shortcut right now is to use the mentioned library? By global I mean when window is out of focus, e.g. minimized

adrientetar commented 1 year ago

For api reference, see QShortcut and QKeyReference https://doc.qt.io/qt-5/qshortcut.html QKeyReference has a constructor that can take between 1-4 keys, so no need for vector or set.

okushnikov commented 3 weeks ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.