feat: close main window when (meta and) escape is pressed (closes #44)

Also added a KeyCode class to make keycode management nicer
This commit is contained in:
Louis Pontoise committed 2019-10-25 13:03:03 +09:00
1 parent ce6f6aa223
commit b6e4826e40
3 files changed
+36 -12

No files matched your search

+16 -1
View File
@@ -1,5 +1,14 @@
import Cocoa
enum KeyCode: UInt16 {
case escape = 53
case command = 55
case capsLock = 57
case tab = 58
case control = 59
case function = 63
}
class Keyboard {
static func listenToGlobalEvents(_ delegate: Application) {
listenToGlobalKeyboardEvents(delegate)
@@ -30,8 +39,10 @@ func keyboardHandler(_ cgEvent: CGEvent, _ delegate: Application) -> Unmanaged<C
if cgEvent.type == .keyDown || cgEvent.type == .keyUp || cgEvent.type == .flagsChanged {
if let event = NSEvent(cgEvent: cgEvent) {
let keyDown = event.type == .keyDown
let optionKeyEvent = event.keyCode == Preferences.metaKeyCode
let keycode = KeyCode(rawValue: event.keyCode)
let optionKeyEvent = keycode == Preferences.metaKeyCode
let tabKeyEvent = event.keyCode == Preferences.tabKey
let escKeyEvent = keycode == KeyCode.escape
if optionKeyEvent && event.modifiersDown([Preferences.metaModifierFlag!]) {
delegate.keyDownMeta()
} else if tabKeyEvent && event.modifiersDown([Preferences.metaModifierFlag!]) && keyDown {
@@ -42,6 +53,10 @@ func keyboardHandler(_ cgEvent: CGEvent, _ delegate: Application) -> Unmanaged<C
delegate.keyDownMetaShiftTab()
// focused app will not receive the event (will not press tab key in that app)
return nil
} else if escKeyEvent && event.modifiersDown([Preferences.metaModifierFlag!]) && keyDown {
delegate.keyDownMetaEsc()
// focused app will not receive the event (will not press tab key in that app)
return nil
} else if optionKeyEvent && !keyDown {
delegate.keyUpMeta()
}
+8 -8
View File
@@ -7,7 +7,7 @@ let defaults = [
"maxThumbnailsPerRow": "4",
"iconSize": "32",
"fontHeight": "15",
"tabKey": "48",
"tabKey": String(KeyCode.tab.rawValue),
"metaKey": Preferences.metaKeyArray[0],
"windowDisplayDelay": "0",
"theme": Preferences.themeArray[0]
@@ -31,19 +31,19 @@ class Preferences {
static var tabKey: UInt16?
static var highlightBorderColor: NSColor?
static var highlightBackgroundColor: NSColor?
static var metaKeyCode: UInt16?
static var metaKeyCode: KeyCode?
static var metaModifierFlag: NSEvent.ModifierFlags?
static var windowDisplayDelay: DispatchTimeInterval?
static var windowCornerRadius: CGFloat?
static var font: NSFont?
static var themeArray = [" macOS", "❖ Windows 10"]
static var metaKeyArray = ["⌥ option", "⌃ control", "⌘ command", "⇪ caps lock", "fn"]
static var metaKeyMap: [String: (UInt16, NSEvent.ModifierFlags)] = [
metaKeyArray[0]: (58, .option),
metaKeyArray[1]: (59, .control),
metaKeyArray[2]: (55, .command),
metaKeyArray[3]: (57, .capsLock),
metaKeyArray[4]: (63, .function),
static var metaKeyMap: [String: (KeyCode, NSEvent.ModifierFlags)] = [
metaKeyArray[0]: (KeyCode.tab, .option),
metaKeyArray[1]: (KeyCode.control, .control),
metaKeyArray[2]: (KeyCode.command, .command),
metaKeyArray[3]: (KeyCode.capsLock, .capsLock),
metaKeyArray[4]: (KeyCode.function, .function),
]
private static let defaultsFile = fileFromPreferencesFolder("alt-tab-macos-defaults.json")
+12 -3
View File
@@ -93,13 +93,17 @@ class Application: NSApplication, NSApplicationDelegate, NSWindowDelegate {
openWindows.count > selectedOpenWindow ? openWindows[selectedOpenWindow] : nil
}
func closeThumbnailsPanel() {
thumbnailsPanel!.orderOut(nil)
appIsBeingUsed = false
isFirstSummon = true
}
func focusSelectedWindow(_ window: OpenWindow?) {
workItems.forEach({ $0.cancel() })
workItems.removeAll()
window?.focus()
thumbnailsPanel!.orderOut(nil)
appIsBeingUsed = false
isFirstSummon = true
closeThumbnailsPanel()
}
func keyDownMeta() {
@@ -118,6 +122,11 @@ class Application: NSApplication, NSApplicationDelegate, NSWindowDelegate {
selectOtherCell(-1)
}
func keyDownMetaEsc() {
debugPrint("meta+esc down")
closeThumbnailsPanel()
}
func keyUpMeta() {
debugPrint("meta up")
if appIsBeingUsed {