Hi, I want that when I press a MAC key (any from A...Z) I perform an action (without having to press ENTER or any other key or button), for example, change the text of a button.
After several hours of searching the internet I only find deprecated code for UIKit (which no longer works with SwiftUI) and I can't find a way to accomplish the task I want.
The closest I came to achieving it is with the following code that what it does is detect when a special key such as CMD, SHIFT, SHIFT, OPTION or CTRL is pressed, then it does perform the action that it programs, but what interests me is to press any key on the keyboard, not just the special ones.
Does anyone know how to do it for someone. New to this?
Thaks.
import Cocoa
import SwiftUI
class ViewController: NSViewController {
@IBOutlet weak var boton: NSButtonCell!
var x = 18 // entero
override func viewDidLoad()
{
super.viewDidLoad()
NSEvent.addLocalMonitorForEvents(matching: .flagsChanged, handler: commandKey(evt:))
}
func commandKey(evt: NSEvent) -> NSEvent
{
if evt.modifierFlags.contains(.control){
if x > 17 || x < 21 {
boton.title = "Road 1"
x = x + 1
}
if x > 20 {
boton.title = "Road 2"
x = x - 10
}
}
return evt
}
}