Swift - keycode keyboardReturn not working - swift

I have hotkeys on my app when using a physical keyboard and all the keys work fine except for the return key. Is there a reason why or a workaround to make this work? Thanks.
extension testViewController {
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
guard let key = presses.first?.key else {return}
if key.keyCode == .keyboardReturn {
print("KEY PRESSED \(key.debugDescription)")
}
}
}

There are two keycodes for return which are .keyboardReturn and .keyboardReturnOrEnter
I just had to use .keyboardReturnOrEnter which I didn't see at the time.

Related

Key down event handler running multiple times in Swift

I am trying to do something when a given key is pressed in a macOS App. First, I ran into a problem where the keyDown event was detected multiple times on each press, therefore executing the handler multiple times. As per a suggestion, I added code to check whether the event is a repeat and it seemed to work at the time. However, this solution seems to work only some of the time, other times the event is getting detected multiple times. Also, I can't seem find a pattern in situations when it works and when it doesn't.What might be the problem and how could I fix it.
Code:
override func viewDidLoad() {
super.viewDidLoad()
NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: checkKeyDown(event:))
}
func checkKeyDown(event: NSEvent) -> NSEvent{
if event.isARepeat == false{
if event.keyCode == 36{
print("Hello World!")
}
}
return event
}
Removing the event monitor when the window closes seems to have fixed this issue.
var numKeyDown : Any?
override func viewDidLoad() {
super.viewDidLoad()
numKeyDown = NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: checkKeyDown(event:))
}
override func viewWillDisappear(){
if let numMonitor = self.numKeyDown {
NSEvent.removeMonitor(numMonitor)
}
}
func checkKeyDown(event: NSEvent) -> NSEvent{
if event.isARepeat == false{
if event.keyCode == 36{
print("Hello World!")
}
}
return event
}

UIPress.key is nil

You may experience a problem where UIPress.key is nil when you are building a Catalyst app for macOS 11 Big Sur and trying to evaluate keyboard input. The code to reproduce the issue is following (in any UIViewController):
#if targetEnvironment(macCatalyst)
override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
//
for press in presses {
guard let key = press.key else { continue }
NSLog("Key Pressed: %#", key.charactersIgnoringModifiers) // <-- will never be logged on Big Sur
}
}
#endif
Running this with Catalina will work, but fails on Big Sur. I have opened a bug report to Apple since I thought this is a bug with Big Sur, but the answer to solve the problem was quite easy:
Simply implement the override for pressesBegan, and it starts to work. So, adding this:
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
//
for press in presses {
guard let key = press.key else { continue }
NSLog("Key press began: %#", key.charactersIgnoringModifiers)
}
}
and the issue is solved.
That's it! I want to share it here, because you may suffer from same issue. But I have also a question to you: For me it's somewhat weird that I need to implement another override to make the desired override working. Do you know the reason here? Is this anywhere documented?
Thanks much.
Also important to note that if you callsuper.pressesEnded(presses, with: event) before accessing the value of UIPress.key then UIPress.key will be nil.

Best strategy in swift to detect keyboad input in NSViewController

I want to detect keyboard input in my NSViewController.
The idea is to have several actions performed if the user presses certain keys followed by ENTER/RETURN.
I have checked if keyDown would be a appropriate way. But I would receive an event any time the user has pressed a key.
I also have though on using an NSTextField, set it to hidden and let it have the focus.
But maybe there are other better solution.
Any ideas?
Thanks
I've finally got a solution that I like.
First it has nothing todo with any hidden UI Elements but rather let the viewcontroller detect keyboard input.
var monitor: Any?
var text = ""
override func viewDidLoad() {
super.viewDidLoad()
self.monitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: myKeyDownEvent)
}
override func viewWillDisappear() {
//Clean up in case your ViewController can be closed an reopened
if let monitor = self.monitor {
NSEvent.removeMonitor(monitor)
}
}
// Detect each keyboard event
func myKeyDownEvent(event: NSEvent) -> NSEvent {
// keyCode 36 is for detect RETURN/ENTER
if event.keyCode == 36 {
print(text)
text = ""
} else {
text.append( event.characters! )
}
return event
}

How to properly test against certain values in NSEventModifierFlags via Swift?

When using the flagsChanged(with event: NSEvent) method of the NSResponder class, how do I test against the event.modifierFlags option set for individual modifier keys being pressed/released?
I have found a solution, here I check for the shift key:
override func flagsChanged(with event: NSEvent)
{
if event.modifierFlags.contains(.shift)
{
// shift pressed
}
if event.modifierFlags.contains(.shift) == false
{
// shift released
}
}

Animations not working in swift playgrounds after button pressed

I have a mouse down function that calls a function of another class in which the instance is named formation
override public func mouseDown(with event: NSEvent) {
let location = event.location(in: self)
let touchedNode = self.atPoint(location)
print(location)
else if touchedNode.name == "navRight"
{
formation.pageOne()
}
}
and with that line i call the pageOne() function
func pageOne()
{
print("in page one")
earthNode.runAction(cpalAccessor.arcRoll())
spaceNode.runAction(cpalAccessor.arcRoll())
}
and the output is simply in page one and the animations do not carry out. Can someone help me with this?