Is there an "official" way to get a string representation of the key? #93
-
|
I am trying to have a comma-delimited string representation of all of the keys a user has pressed over a given period of time (Ex: An 'add keys to list' screen that updates a label with the running list). I have tried using the Just wanted to get some input before I end up just writing some sort of "KeyCode -> String representation" hardcoded dictionary/class |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
While I am still interested in the answer to this, I did end up just writing my own translation util class for my program. Happy to fork and PR that, if you'd find it helpful :) |
Beta Was this translation helpful? Give feedback.
-
|
Hello! Thanks for asking! As far as I understand, This is a problem which seems simple but may include some subtle edge cases. For example, the same key can have different names on different OSes: Alt on Windows is Option on macOS; Win key on Windows is Command on macOS, Super on some Linux distributions, and Meta on others; and for some absolutely inexplicable reason Microsoft recently announced a new Copilot key which, as far as I understand, is just a redesigned Win key, or maybe not, I'm not really sure. Also, the same key may have different meanings depending on the keyboard state, e.g. if the Function key is pressed or not. I think writing some kind of a dictionary which maps keys to names depending on the OS is good enough while not being technically 100% correct for all possible cases. Also, I don't think this feature should be included in SharpHook itself. As I've said it's too difficult to write a general-enough solution which works for vastly different users and use cases, and then maintain it. You should instead implement the mapping which works for you and not think about some arcane keyboard peculiarities. I'm really glad that you offered to provide such a feature, this is really appreciated! If you'd like, you can share a snippet or a gist with the mappings - this may be helpful for other users which would like to have something similar. |
Beta Was this translation helpful? Give feedback.
Hello! Thanks for asking!
As far as I understand,
KeyCharof theKeyTypedevent doesn't really suit you, because you don't want a character that's typed by the key press. Rather, you want the actual name of the key (i.e. what the key looks like on a physical keyboard). Unfortunately, there's no "official" way to get such a string representation of a key.This is a problem which seems simple but may include some subtle edge cases. For example, the same key can have different names on different OSes: Alt on Windows is Option on macOS; Win key on Windows is Command on macOS, Super on some Linux distributions, and Meta on others; and for some absolutely inexplicable reason Microsoft recently …