Skip to content

Commit

Permalink
feat: close main window when (meta and) escape is pressed (closes #44)
Browse files Browse the repository at this point in the history
Also added a KeyCode class to make keycode management nicer
  • Loading branch information
Louis Pontoise committed Oct 25, 2019
1 parent ce6f6aa commit b6e4826
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
17 changes: 16 additions & 1 deletion alt-tab-macos/logic/Keyboard.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down
16 changes: 8 additions & 8 deletions alt-tab-macos/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand Down
15 changes: 12 additions & 3 deletions alt-tab-macos/ui/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit b6e4826

Please sign in to comment.