Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift macOS检测按键按下时鼠标按钮是否按下_Swift_Macos_Keydown_Mousedown_Nsevent - Fatal编程技术网

Swift macOS检测按键按下时鼠标按钮是否按下

Swift macOS检测按键按下时鼠标按钮是否按下,swift,macos,keydown,mousedown,nsevent,Swift,Macos,Keydown,Mousedown,Nsevent,在keyDown(带有event:NSEvent)中,我可以知道是否按了OPT、CMD、CTRL、Shift,并使用以下代码 override func keyDown(with event: NSEvent) { let shiftPressed = (event.modifierFlags.rawValue & NSEventModifierFlags.shift.rawValue != 0) let commandPressed = (event.modifierF

keyDown(带有event:NSEvent)
中,我可以知道是否按了OPT、CMD、CTRL、Shift,并使用以下代码

override func keyDown(with event: NSEvent) {
    let shiftPressed = (event.modifierFlags.rawValue & NSEventModifierFlags.shift.rawValue != 0)
    let commandPressed = (event.modifierFlags.rawValue & NSEventModifierFlags.command.rawValue != 0)
    let optionPressed = (event.modifierFlags.rawValue & NSEventModifierFlags.option.rawValue != 0)
    let controlPressed = (event.modifierFlags.rawValue & NSEventModifierFlags.control.rawValue != 0)
    ...

如何检测鼠标左键是否在向下键中向上或向下?

正如您可能注意到的,
向下键:
跟踪键盘而不是鼠标,因此您需要跟踪鼠标。然后:

捕捉鼠标事件并跟踪
mouseDown:
,在其内部设置一个标志,您可以在
mouseUp:
中重置该标志,然后在
键下:
中测试该标志

-----编辑----

您也可以使用
的方法
按下鼠标按钮:
NSEvent

轮询鼠标(无论如何轮询并不总是一个好主意),我相信这是标准方式。同样,当鼠标事件发生时,您不能查询键盘状态,当按键事件发生时,您不能轮询鼠标状态:-)由您设置变量来协调它们。