Home > Software design >  How to detect Fn key press in Swift?
How to detect Fn key press in Swift?

Time:11-09

I want to detect when the user presses the Fn key and do some tasks. I tried the below but it doesn't work:

if event.keyCode == kVK_Function {
   print("fn key pressed")
}

I have similar code for other keys like left bracket, right bracket, slash, alphabets, and numbers. For these, similar code, as shown above, works fine but it doesn't work for Fn key. I think this is handled differently.

CodePudding user response:

You need to check the NSEvent's modifierFlags:

if event.modifierFlags.contains(.function) {
   print("fn key pressed")
}
  • Related