Changing a Boolean when a key is pressed.

Does anyone know how I would be able to change a Boolean when a key is pressed on a keyboard?
15 Replies
nouun
nouunβ€’15mo ago
Can you give any more information? macOS or iOS? Is this to do with a text box or just a generic keypress? Do you want it to happen outside the application if on macOS? You don’t need to ping coaches 6 minutes after the post was created.
bobtheguy_32392
bobtheguy_32392OPβ€’15mo ago
So basicly if i were to give more information, say you were on an ipad with a keyboard attcehd to it, how would i be able to change a Boolean if i were to press one of the keys on the keyboard-? (I hope this gives some more information)
nouun
nouunβ€’15mo ago
SwiftUI or UIKit?
bobtheguy_32392
bobtheguy_32392OPβ€’15mo ago
SwiftUI
nouun
nouunβ€’15mo ago
Apple Developer Documentation
keyboardShortcut(_:modifiers:) | Apple Developer Documentation
Defines a keyboard shortcut and assigns it to the modified control.
nouun
nouunβ€’15mo ago
It’s the β€œSwiftUI” way to do it.
bobtheguy_32392
bobtheguy_32392OPβ€’15mo ago
I think it would work, Thank you!
nouun
nouunβ€’15mo ago
The other option would be something like this:
.onAppear() {
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event -> NSEvent? in
if event.keyCode == 53 { // if esc pressed
// Do something here

return nil // Do not do "beep" sound
}

// Return original event if not handled
return event
}
}
.onAppear() {
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { event -> NSEvent? in
if event.keyCode == 53 { // if esc pressed
// Do something here

return nil // Do not do "beep" sound
}

// Return original event if not handled
return event
}
}
bobtheguy_32392
bobtheguy_32392OPβ€’15mo ago
Alright, thank you for 2 different options! πŸ˜€
nouun
nouunβ€’15mo ago
Stack Overflow
SwiftUI iOS - how to capture hardware key events
I’m new to iOS development. Following a tutorial I have created a simple calculator using SwiftUI. I have a keyboard attached to my iPad, and I would like to be able to enter values using the keyb...
nouun
nouunβ€’15mo ago
One of them should hopefully work for your use case.
bobtheguy_32392
bobtheguy_32392OPβ€’15mo ago
Wow, thank you for all the options man! it's really appreciated.
πŸ‡¦πŸ‡ΊπŸ¦…βœπŸ”₯ π•°π–•π–Žπ–π–šπ–˜ πŸ”₯βœπŸ¦…πŸ‡¦πŸ‡ΊπŸ‘‘Β©β„’
Bro just being utterly flabbergasted at the help you can receive through this server
Want results from more Discord servers?
Add your server