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
}
}
Related
To scroll a SKNode (called scroller) within a SKScene (called menuScene), I'm doing the following, which works perfectly but I'm not sure if it's the correct way to achieve this.
Within the NSViewController class, I'm overriding the scrollWheel event to call the scene's own scrollWheel event:
override func scrollWheel(with event: NSEvent) {
skView?.scene?.scrollWheel(with: event)
super.scrollWheel(with: event)
}
Then, within menuScene, the function is simply:
override func scrollWheel(with event: NSEvent) {
scroller.position.x = scroller.position.x + (event.deltaX*3)
}
This works perfectly, but is there a better way to achieve the same goal?
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.
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
}
This question already has answers here:
How to hook/remap an arbitrary keyboard event on OSX?
(2 answers)
Closed 3 years ago.
I have written this code. My problem is, the keyDown function only listens to certain keys (letters, numbers, space, characters, Tab.). However, it completely ignores keys like caps lock and ⇧ Shift and Control. How can I make it listen to these keys?
class GameClient: NSView {
override var acceptsFirstResponder: Bool {
return true
}
override func keyDown(with event: NSEvent) {
print(event.keyCode)
}
override func keyUp(with event: NSEvent) {
}
}
The answers in the suggested dupe don't fully answer my question. I don't want to see if ⇧ Shift was being pressed while I was pressing A, I want to detect when ⇧ Shift alone is pressed, for example.
The question is how to capture a standalone shift key event, rather than detecting if shift is being pressed while another key is. You want to do this:
override func flagsChanged(with event: NSEvent) {
print(event.keyCode)
}
override func keyDown(with event: NSEvent) {
print(event.keyCode)
}
override func keyUp(with event: NSEvent) {
}
Using NSTextField on Custom NSTableCellView.
When the user presses anywhere on the cell, I would like NSTextField to become the first responder.
When I do, the text disappears, and the user needs to press any keyboard key for it to come back, how to disable this behavior?
Code:
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
if (event.clickCount == 2) {
self.beginEditing()
} else {
self.endEditing()
}
}
private func beginEditing() {
self.lblTitle.isEditable = true
self.window?.makeFirstResponder(self.lblTitle)
self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear
}
private func endEditing() {
self.lblTitle.isEditable = false
self.lblTitle.backgroundColor = Colors.red
}
Removing this lines solved it
self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear