|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +@_exported import AppKit |
| 14 | + |
| 15 | +extension NSEvent { |
| 16 | + public struct SpecialKey : RawRepresentable, Equatable, Hashable { |
| 17 | + public init(rawValue: Int) { |
| 18 | + self.rawValue = rawValue |
| 19 | + } |
| 20 | + public let rawValue: Int |
| 21 | + public var unicodeScalar: Unicode.Scalar { |
| 22 | + return Unicode.Scalar(rawValue)! |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Returns nil if the receiver is not a "special" key event. |
| 27 | + open var specialKey: SpecialKey? { |
| 28 | + guard let unicodeScalars = charactersIgnoringModifiers?.unicodeScalars else { |
| 29 | + return nil |
| 30 | + } |
| 31 | + guard unicodeScalars.count == 1 else { |
| 32 | + return nil |
| 33 | + } |
| 34 | + guard let codePoint = unicodeScalars.first?.value else { |
| 35 | + return nil |
| 36 | + } |
| 37 | + switch codePoint { |
| 38 | + case 0x0003: |
| 39 | + return .enter |
| 40 | + |
| 41 | + case 0x0008: |
| 42 | + return .backspace |
| 43 | + |
| 44 | + case 0x0009: |
| 45 | + return .tab |
| 46 | + |
| 47 | + case 0x000a: |
| 48 | + return .newline |
| 49 | + |
| 50 | + case 0x000c: |
| 51 | + return .formFeed |
| 52 | + |
| 53 | + case 0x000d: |
| 54 | + return .carriageReturn |
| 55 | + |
| 56 | + case 0x0019: |
| 57 | + return .backTab |
| 58 | + |
| 59 | + case 0x007f: |
| 60 | + return .delete |
| 61 | + |
| 62 | + case 0x2028: |
| 63 | + return .lineSeparator |
| 64 | + |
| 65 | + case 0x2029: |
| 66 | + return .paragraphSeparator |
| 67 | + |
| 68 | + case 0xF700..<0xF900: |
| 69 | + return SpecialKey(rawValue: Int(codePoint)) |
| 70 | + |
| 71 | + default: |
| 72 | + return nil |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension NSEvent.SpecialKey { |
| 78 | + |
| 79 | + static public let upArrow = NSEvent.SpecialKey(rawValue: 0xF700) |
| 80 | + static public let downArrow = NSEvent.SpecialKey(rawValue: 0xF701) |
| 81 | + static public let leftArrow = NSEvent.SpecialKey(rawValue: 0xF702) |
| 82 | + static public let rightArrow = NSEvent.SpecialKey(rawValue: 0xF703) |
| 83 | + static public let f1 = NSEvent.SpecialKey(rawValue: 0xF704) |
| 84 | + static public let f2 = NSEvent.SpecialKey(rawValue: 0xF705) |
| 85 | + static public let f3 = NSEvent.SpecialKey(rawValue: 0xF706) |
| 86 | + static public let f4 = NSEvent.SpecialKey(rawValue: 0xF707) |
| 87 | + static public let f5 = NSEvent.SpecialKey(rawValue: 0xF708) |
| 88 | + static public let f6 = NSEvent.SpecialKey(rawValue: 0xF709) |
| 89 | + static public let f7 = NSEvent.SpecialKey(rawValue: 0xF70A) |
| 90 | + static public let f8 = NSEvent.SpecialKey(rawValue: 0xF70B) |
| 91 | + static public let f9 = NSEvent.SpecialKey(rawValue: 0xF70C) |
| 92 | + static public let f10 = NSEvent.SpecialKey(rawValue: 0xF70D) |
| 93 | + static public let f11 = NSEvent.SpecialKey(rawValue: 0xF70E) |
| 94 | + static public let f12 = NSEvent.SpecialKey(rawValue: 0xF70F) |
| 95 | + static public let f13 = NSEvent.SpecialKey(rawValue: 0xF710) |
| 96 | + static public let f14 = NSEvent.SpecialKey(rawValue: 0xF711) |
| 97 | + static public let f15 = NSEvent.SpecialKey(rawValue: 0xF712) |
| 98 | + static public let f16 = NSEvent.SpecialKey(rawValue: 0xF713) |
| 99 | + static public let f17 = NSEvent.SpecialKey(rawValue: 0xF714) |
| 100 | + static public let f18 = NSEvent.SpecialKey(rawValue: 0xF715) |
| 101 | + static public let f19 = NSEvent.SpecialKey(rawValue: 0xF716) |
| 102 | + static public let f20 = NSEvent.SpecialKey(rawValue: 0xF717) |
| 103 | + static public let f21 = NSEvent.SpecialKey(rawValue: 0xF718) |
| 104 | + static public let f22 = NSEvent.SpecialKey(rawValue: 0xF719) |
| 105 | + static public let f23 = NSEvent.SpecialKey(rawValue: 0xF71A) |
| 106 | + static public let f24 = NSEvent.SpecialKey(rawValue: 0xF71B) |
| 107 | + static public let f25 = NSEvent.SpecialKey(rawValue: 0xF71C) |
| 108 | + static public let f26 = NSEvent.SpecialKey(rawValue: 0xF71D) |
| 109 | + static public let f27 = NSEvent.SpecialKey(rawValue: 0xF71E) |
| 110 | + static public let f28 = NSEvent.SpecialKey(rawValue: 0xF71F) |
| 111 | + static public let f29 = NSEvent.SpecialKey(rawValue: 0xF720) |
| 112 | + static public let f30 = NSEvent.SpecialKey(rawValue: 0xF721) |
| 113 | + static public let f31 = NSEvent.SpecialKey(rawValue: 0xF722) |
| 114 | + static public let f32 = NSEvent.SpecialKey(rawValue: 0xF723) |
| 115 | + static public let f33 = NSEvent.SpecialKey(rawValue: 0xF724) |
| 116 | + static public let f34 = NSEvent.SpecialKey(rawValue: 0xF725) |
| 117 | + static public let f35 = NSEvent.SpecialKey(rawValue: 0xF726) |
| 118 | + static public let insert = NSEvent.SpecialKey(rawValue: 0xF727) |
| 119 | + static public let deleteForward = NSEvent.SpecialKey(rawValue: 0xF7028) |
| 120 | + static public let home = NSEvent.SpecialKey(rawValue: 0xF729) |
| 121 | + static public let begin = NSEvent.SpecialKey(rawValue: 0xF72A) |
| 122 | + static public let end = NSEvent.SpecialKey(rawValue: 0xF72B) |
| 123 | + static public let pageUp = NSEvent.SpecialKey(rawValue: 0xF72C) |
| 124 | + static public let pageDown = NSEvent.SpecialKey(rawValue: 0xF72D) |
| 125 | + static public let printScreen = NSEvent.SpecialKey(rawValue: 0xF72E) |
| 126 | + static public let scrollLock = NSEvent.SpecialKey(rawValue: 0xF72F) |
| 127 | + static public let pause = NSEvent.SpecialKey(rawValue: 0xF730) |
| 128 | + static public let sysReq = NSEvent.SpecialKey(rawValue: 0xF731) |
| 129 | + static public let `break` = NSEvent.SpecialKey(rawValue: 0xF732) |
| 130 | + static public let reset = NSEvent.SpecialKey(rawValue: 0xF733) |
| 131 | + static public let stop = NSEvent.SpecialKey(rawValue: 0xF734) |
| 132 | + static public let menu = NSEvent.SpecialKey(rawValue: 0xF735) |
| 133 | + static public let user = NSEvent.SpecialKey(rawValue: 0xF736) |
| 134 | + static public let system = NSEvent.SpecialKey(rawValue: 0xF737) |
| 135 | + static public let print = NSEvent.SpecialKey(rawValue: 0xF738) |
| 136 | + static public let clearLine = NSEvent.SpecialKey(rawValue: 0xF739) |
| 137 | + static public let clearDisplay = NSEvent.SpecialKey(rawValue: 0xF73A) |
| 138 | + static public let insertLine = NSEvent.SpecialKey(rawValue: 0xF73B) |
| 139 | + static public let deleteLine = NSEvent.SpecialKey(rawValue: 0xF73C) |
| 140 | + static public let insertCharacter = NSEvent.SpecialKey(rawValue: 0xF73D) |
| 141 | + static public let deleteCharacter = NSEvent.SpecialKey(rawValue: 0xF73E) |
| 142 | + static public let prev = NSEvent.SpecialKey(rawValue: 0xF73F) |
| 143 | + static public let next = NSEvent.SpecialKey(rawValue: 0xF740) |
| 144 | + static public let select = NSEvent.SpecialKey(rawValue: 0xF741) |
| 145 | + static public let execute = NSEvent.SpecialKey(rawValue: 0xF742) |
| 146 | + static public let undo = NSEvent.SpecialKey(rawValue: 0xF743) |
| 147 | + static public let redo = NSEvent.SpecialKey(rawValue: 0xF744) |
| 148 | + static public let find = NSEvent.SpecialKey(rawValue: 0xF745) |
| 149 | + static public let help = NSEvent.SpecialKey(rawValue: 0xF746) |
| 150 | + static public let modeSwitch = NSEvent.SpecialKey(rawValue: 0xF747) |
| 151 | + |
| 152 | + static public let enter = NSEvent.SpecialKey(rawValue: 0x0003) |
| 153 | + static public let backspace = NSEvent.SpecialKey(rawValue: 0x0008) |
| 154 | + static public let tab = NSEvent.SpecialKey(rawValue: 0x0009) |
| 155 | + static public let newline = NSEvent.SpecialKey(rawValue: 0x000a) |
| 156 | + static public let formFeed = NSEvent.SpecialKey(rawValue: 0x000c) |
| 157 | + static public let carriageReturn = NSEvent.SpecialKey(rawValue: 0x000d) |
| 158 | + static public let backTab = NSEvent.SpecialKey(rawValue: 0x0019) |
| 159 | + static public let delete = NSEvent.SpecialKey(rawValue: 0x007f) |
| 160 | + static public let lineSeparator = NSEvent.SpecialKey(rawValue: 0x2028) |
| 161 | + static public let paragraphSeparator = NSEvent.SpecialKey(rawValue: 0x2029) |
| 162 | +} |
0 commit comments