Duck
Duck

Reputation: 36003

Why is this functionKey selected when I press F1, F2, etc?

I am reading the keyboard shortcuts typed inside a NSTextField.

When I press, for example, shift + option + F2, this method

class func convertToString(event:NSEvent) -> String {

var shortcut = ""

let intersection = event.modifierFlags.intersection(.deviceIndependentFlagsMask)

if intersection.contains(.control) {
  shortcut.append("^ ")
}
if intersection.contains(.option) {
  shortcut.append("⌥ ")
}
if intersection.contains(.command) {
  shortcut.append("⌘ ")
}
if intersection.contains(.shift) {
  shortcut.append("⇧ ")
}
if intersection.contains(.function) {
  shortcut.append("fn ")
}

let character = keysCodesToCharacters[event.keyCode]!
shortcut.append(character)

return shortcut

}

will evaluate true for

if intersection.contains(.function)

In theory this tests for the function key but the most strange part is that event.keycode comes as 120 that corresponds to F2.

So I don't need to do this test at all to test for the function keys F1 to F12.

The big question is: what is the purpose of this .function. What is this testing?

I thought of the fn keys on the mac keyboard but these keys are not detectable and when I press any key from F1 to F12, this method gives me the keycode of the key plus this function true.

What I mean is this: when I press F2, for example, I receive event.keycode, meaning that F2 was pressed and also functionKey (63) as true? Why the redundancy?

Upvotes: 1

Views: 217

Answers (1)

Willeke
Willeke

Reputation: 15623

Apple's documentation isn't what is used to be.

If the documentation doesn't help, check the header. Copied from NSEvents.h:

    NSEventModifierFlagFunction           = 1 << 23, // Set if any function key is pressed.

NSEventModifierFlagFunction was NSFunctionKeyMask, documentation:

NSFunctionKeyMask

Set if any function key is pressed. The function keys include the F keys at the top of most keyboards (F1, F2, and so on) and the navigation keys in the center of most keyboards (Help, Forward Delete, Home, End, Page Up, Page Down, and the arrow keys).

Apparently the .function flag is also set when the fn-key is pressed (on my Apple keyboard, my Logitech keyboard handles the fn-key internally).

Upvotes: 3

Related Questions