2016-07-12 09:13:40 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
2016-07-12 00:24:40 +03:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class KeyUtils {
|
|
|
|
|
2017-08-02 22:18:23 +03:00
|
|
|
static func isControlCode(key: String) -> Bool {
|
2017-10-22 22:43:03 +03:00
|
|
|
guard key.count == 1 else {
|
2017-08-02 22:18:23 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let firstChar = key.utf16.first else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstChar < 32 && firstChar > 0
|
|
|
|
}
|
|
|
|
|
2016-09-25 18:50:33 +03:00
|
|
|
static func isSpecial(key: String) -> Bool {
|
2017-10-22 22:43:03 +03:00
|
|
|
guard key.count == 1 else {
|
2016-07-12 00:24:40 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if let firstChar = key.utf16.first {
|
2017-05-17 22:57:16 +03:00
|
|
|
return specialKeys.keys.contains(Int(firstChar))
|
2016-07-12 00:24:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-26 10:26:45 +03:00
|
|
|
static func namedKey(from key: String) -> String {
|
|
|
|
if let firstChar = key.utf16.first, let special = specialKeys[Int(firstChar)] {
|
|
|
|
return special
|
2016-07-12 00:24:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 22:57:16 +03:00
|
|
|
|
|
|
|
fileprivate let specialKeys = [
|
|
|
|
NSUpArrowFunctionKey: "Up",
|
|
|
|
NSDownArrowFunctionKey: "Down",
|
|
|
|
NSLeftArrowFunctionKey: "Left",
|
|
|
|
NSRightArrowFunctionKey: "Right",
|
|
|
|
NSInsertFunctionKey: "Insert",
|
|
|
|
0x7F: "BS", // "delete"-key
|
|
|
|
NSDeleteFunctionKey: "Del", // "Fn+delete"-key
|
|
|
|
NSHomeFunctionKey: "Home",
|
|
|
|
NSBeginFunctionKey: "Begin",
|
|
|
|
NSEndFunctionKey: "End",
|
|
|
|
NSPageUpFunctionKey: "PageUp",
|
|
|
|
NSPageDownFunctionKey: "PageDown",
|
|
|
|
NSHelpFunctionKey: "Help",
|
|
|
|
NSF1FunctionKey: "F1",
|
|
|
|
NSF2FunctionKey: "F2",
|
|
|
|
NSF3FunctionKey: "F3",
|
|
|
|
NSF4FunctionKey: "F4",
|
|
|
|
NSF5FunctionKey: "F5",
|
|
|
|
NSF6FunctionKey: "F6",
|
|
|
|
NSF7FunctionKey: "F7",
|
|
|
|
NSF8FunctionKey: "F8",
|
|
|
|
NSF9FunctionKey: "F9",
|
|
|
|
NSF10FunctionKey: "F10",
|
|
|
|
NSF11FunctionKey: "F11",
|
|
|
|
NSF12FunctionKey: "F12",
|
|
|
|
NSF13FunctionKey: "F13",
|
|
|
|
NSF14FunctionKey: "F14",
|
|
|
|
NSF15FunctionKey: "F15",
|
|
|
|
NSF16FunctionKey: "F16",
|
|
|
|
NSF17FunctionKey: "F17",
|
|
|
|
NSF18FunctionKey: "F18",
|
|
|
|
NSF19FunctionKey: "F19",
|
|
|
|
NSF20FunctionKey: "F20",
|
|
|
|
NSF21FunctionKey: "F21",
|
|
|
|
NSF22FunctionKey: "F22",
|
|
|
|
NSF23FunctionKey: "F23",
|
|
|
|
NSF24FunctionKey: "F24",
|
|
|
|
NSF25FunctionKey: "F25",
|
|
|
|
NSF26FunctionKey: "F26",
|
|
|
|
NSF27FunctionKey: "F27",
|
|
|
|
NSF28FunctionKey: "F28",
|
|
|
|
NSF29FunctionKey: "F29",
|
|
|
|
NSF30FunctionKey: "F30",
|
|
|
|
NSF31FunctionKey: "F31",
|
|
|
|
NSF32FunctionKey: "F32",
|
|
|
|
NSF33FunctionKey: "F33",
|
|
|
|
NSF34FunctionKey: "F34",
|
|
|
|
NSF35FunctionKey: "F35",
|
2017-07-31 23:55:27 +03:00
|
|
|
0x09: "Tab",
|
2017-05-17 22:57:16 +03:00
|
|
|
0x19: "Tab",
|
|
|
|
]
|