Permalink
Browse files

add key configuration

1 parent 97c4257 commit 22aeb01b99bbf817d37e72fb0be4d1c71ccf66d7 @iMasanari committed Oct 4, 2016
@@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
- <key>LSUIElement</key>
- <true/>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
@@ -24,6 +22,8 @@
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
+ <key>LSUIElement</key>
+ <true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016年 eikana. All rights reserved.</string>
<key>NSPrincipalClass</key>
@@ -11,6 +11,8 @@ import Cocoa
var statusItem = NSStatusBar.system().statusItem(withLength: CGFloat(NSVariableStatusItemLength))
var loginItem = NSMenuItem()
+var oneShotModifiers: [CGKeyCode: KeyboardShortcut] = [:]
+
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var windowController : NSWindowController?
@@ -38,6 +40,26 @@ class AppDelegate: NSObject, NSApplicationDelegate {
checkUpdate()
}
+ if let oneShotModifiersData = userDefaults.object(forKey: "oneShotModifiers") {
+ oneShotModifiers = [:]
+
+ (oneShotModifiersData as! [AnyObject]).forEach({ (val) in
+ let input = CGKeyCode(val["input"] as! Int)
+ let output = KeyboardShortcut(dictionary: val["output"] as! [AnyHashable: Any])
+
+ oneShotModifiers[input] = output
+ })
+
+ }
+ else {
+ oneShotModifiers = [
+ 55: KeyboardShortcut(keyCode: 102),
+ 54: KeyboardShortcut(keyCode: 104)
+ ]
+
+ saveKeyMappings()
+ }
+
let menu = NSMenu()
statusItem.title = ""
statusItem.highlightMode = true
@@ -55,12 +77,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(withTitle: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "")
_ = KeyEvent()
+
+// openPreferences()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
+ func applicationDidResignActive(_ notification: Notification) {
+ selectKeyTextField?.textField.window?.makeFirstResponder(nil)
+ selectKeyTextField = nil
+ }
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
openPreferences()
return false
@@ -112,3 +140,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
NSApplication.shared().terminate(self)
}
}
+
+func saveKeyMappings() {
+ UserDefaults.standard.set(oneShotModifiers.map {[
+ "input": Int($0.0),
+ "output": $0.1.toDictionary()
+ ]} , forKey: "oneShotModifiers")
+}
Oops, something went wrong.
@@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
- <string>1.1.1</string>
+ <string>2.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
@@ -8,14 +8,8 @@
import Cocoa
-let singleModifierKeyUpActions: [Int64: () -> Void] = [
- 55 /* left command */: postKeyboardEvent(102 /* jis-eisuu */),
- 54 /* right command */: postKeyboardEvent(104 /* jis-kana */),
- // 59 /* left control */: postKeyboardEvent(49 /* space */, flags: CGEventFlags.maskCommand)
-]
-
class KeyEvent: NSObject {
- var keyCode: Int64? = nil
+ var keyCode: CGKeyCode? = nil
override init() {
super.init()
@@ -88,48 +82,39 @@ class KeyEvent: NSObject {
}
func callback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent, refcon: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
- if [.flagsChanged].contains(type) {
- let keyCode = event.getIntegerValueField(.keyboardEventKeycode)
+ // if type == CGEventType.keyDown {print(KeyboardShortcut(event).toString())}
+
+ if let textFeild = selectKeyTextField {
+ self.keyCode = nil
+
+ if type == CGEventType.keyDown {
+ textFeild.textField.stringValue = KeyboardShortcut(event).toString()
+
+ selectKeyTextField = (
+ textField: textFeild.textField,
+ KeyboardShortcut(keyCode: CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode)), flags: event.flags)
+ )
+
+ return nil
+ }
+ }
+
+ if type == CGEventType.flagsChanged {
+ let keyCode = CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode))
- if let action = singleModifierKeyUpActions[keyCode] {
+ if let shortcut = oneShotModifiers[keyCode] {
if event.flags.rawValue & modifierMasks[keyCode]!.rawValue != 0 {
self.keyCode = keyCode
}
else if keyCode == self.keyCode {
- action()
+ shortcut.postEvent()
}
}
}
else {
self.keyCode = nil
}
-
return Unmanaged.passRetained(event)
}
}
-let modifierMasks: [Int64: CGEventFlags] = [
- 54: CGEventFlags.maskCommand,
- 55: CGEventFlags.maskCommand,
- 56: CGEventFlags.maskShift,
- 60: CGEventFlags.maskShift,
- 59: CGEventFlags.maskControl,
- 62: CGEventFlags.maskControl,
- 58: CGEventFlags.maskAlternate,
- 61: CGEventFlags.maskAlternate,
- 63: CGEventFlags.maskSecondaryFn
-]
-
-func postKeyboardEvent(_ virtualKey: CGKeyCode, flags: CGEventFlags = CGEventFlags()) -> () -> Void {
- let loc = CGEventTapLocation.cghidEventTap
- let keyDownEvent = CGEvent(keyboardEventSource: nil, virtualKey: virtualKey, keyDown: true)!
- let keyUpEvent = CGEvent(keyboardEventSource: nil, virtualKey: virtualKey, keyDown: false)!
-
- keyDownEvent.flags = flags
- keyUpEvent.flags = CGEventFlags()
-
- return { () -> Void in
- keyDownEvent.post(tap: loc)
- keyUpEvent.post(tap: loc)
- }
-}
@@ -0,0 +1,78 @@
+//
+// KeyTextField.swift
+// ⌘英かな
+//
+// MIT License
+// Copyright (c) 2016 iMasanari
+//
+
+import Cocoa
+
+class KeyTextField: NSComboBox {
+ /// Custom delegate with other methods than NSTextFieldDelegate.
+
+ override func becomeFirstResponder() -> Bool {
+ let became = super.becomeFirstResponder();
+ if (became) {
+ if let shortcut = oneShotModifiers[tableDataIndex[self.identifier!]!] {
+ selectKeyTextField = (textField: self, shortcut)
+ }
+ else {
+ selectKeyTextField = (textField: self, key: nil)
+ }
+ }
+ return became;
+ }
+
+// override func resignFirstResponder() -> Bool {
+// let resigned = super.resignFirstResponder();
+// if (resigned) {
+// }
+// return resigned;
+// }
+
+ override func textDidEndEditing(_ obj: Notification) {
+ super.textDidEndEditing(obj)
+
+ switch self.stringValue {
+ case "(削除)":
+ self.stringValue = ""
+
+ if selectKeyTextField != nil {
+ selectKeyTextField!.key = nil
+ // selectKeyTextField!.key = KeyboardShortcut(keyCode: 57)
+ }
+ break
+ case "JIS_EISUU":
+ selectKeyTextField = (textField: self, KeyboardShortcut(keyCode: 102))
+ break
+ case "JIS_KANA":
+ selectKeyTextField = (textField: self, KeyboardShortcut(keyCode: 104))
+ break
+ case "⌘SPACE":
+ selectKeyTextField = (textField: self, KeyboardShortcut(keyCode: 49, flags: CGEventFlags.maskCommand))
+ break
+ case "⌃SPACE":
+ selectKeyTextField = (textField: self, KeyboardShortcut(keyCode: 49, flags: CGEventFlags.maskControl))
+ break
+ default:
+ break
+ }
+
+ let key = tableDataIndex[self.identifier!]!
+
+ if let shortcut = selectKeyTextField?.key {
+ self.stringValue = shortcut.toString()
+ oneShotModifiers[key] = shortcut
+ }
+ else {
+ oneShotModifiers.removeValue(forKey: key)
+ }
+
+ saveKeyMappings()
+
+ if selectKeyTextField?.textField.identifier == self.identifier {
+ selectKeyTextField = nil
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 22aeb01

Please sign in to comment.