Getting keyboard events from TextField

Hello,


Is there a way to capture keyboard events from a TextField? I'm trying to make a keyboard shortcut entry field, and need to know whether the user is pressing the Command, Shift, Option, etc key. Looking at the docs it appears I can only get the string the user has typed - is there a way to get more granular keyboard events? Especially on macOS?


Thanks,

Austin

Replies

I think the onCommand(Selector, perform: ()->Void) https://developer.apple.com/documentation/swiftui/view/3367871-oncommand

is made for that but I don't know how to use it or if it is already working.


None of the `onCopyCommand`, `onCutCommand`, `func onDeleteCommand` and so forth seem to be working in beta 5.

Thanks damiaan!


For `onCommand(Selector, action)` I tried a variety of Selectors but it doesn't appear to be working. `onCopyCommand`, etc., doesn't work either (like you warned).


I'm not sure `onCommand(...)` will provide the functionality I need once it's fixed though. I don't see a way to pass data through the .onCommand() selector to the action, so I won't be able to get the NSEvent(s) to grab keys. Let me know if I'm missing something.


For now I'll plan on using AppKit to make this specific part work.


Either way, the `onCommand()` API looks useful - so thanks!