Skip to content

Commit

Permalink
Support Shift key tuyenvm#24, Beep sound on/off tuyenvm#21
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyenvm committed Aug 1, 2019
1 parent cc82aac commit f50b894
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Sources/OpenKey/engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define HAS_CONTROL(data) (data & 0x100)
#define HAS_OPTION(data) (data & 0x200)
#define HAS_COMMAND(data) (data & 0x400)
#define HAS_SHIFT(data) (data & 0x800)
#define HAS_BEEP(data) (data & 0x8000)
#define SET_SWITCH_KEY(data, key) data = (data & 0xFF) | key
#define SET_CONTROL_KEY(data, val) data|=val<<8;
#define SET_OPTION_KEY(data, val) data|=val<<9;
Expand Down
6 changes: 4 additions & 2 deletions Sources/OpenKey/macOS/ModernKey/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

NSBeep();
if (vSwitchKeyStatus & 0x8000)
NSBeep();

[self createStatusBarMenu];

Expand Down Expand Up @@ -259,7 +260,8 @@ - (void)onInputMethodSelected {
intInputMethod = 0;
vLanguage = (int)intInputMethod;
[[NSUserDefaults standardUserDefaults] setInteger:intInputMethod forKey:@"InputMethod"];
NSBeep();
if (vSwitchKeyStatus & 0x8000)
NSBeep();
[self fillData];
[viewController fillData];
}
Expand Down
42 changes: 33 additions & 9 deletions Sources/OpenKey/macOS/ModernKey/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,32 @@
</view>
<font key="titleFont" metaFont="system"/>
</box>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wh2-uV-nSn">
<rect key="frame" x="275" y="371" width="34" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="Wfz-i8-TNp">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onShiftSwitchKey:" target="XfG-lQ-9wD" id="cjW-vM-nFX"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="bJy-t3-bzy">
<rect key="frame" x="403" y="371" width="79" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Kêu beep" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="Lh1-wK-W31">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="onBeepSound:" target="XfG-lQ-9wD" id="t57-pm-xPS"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5e5-3S-Gd8">
<rect key="frame" x="111" y="371" width="82" height="18"/>
<rect key="frame" x="110" y="371" width="34" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title=" Control" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="RSz-p1-5PA">
<buttonCell key="cell" type="check" title="" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="RSz-p1-5PA">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -823,9 +845,9 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Mox-UL-j9u">
<rect key="frame" x="216" y="371" width="78" height="18"/>
<rect key="frame" x="165" y="371" width="34" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title=" Option" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="9YC-ED-crZ">
<buttonCell key="cell" type="check" title="" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="9YC-ED-crZ">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -834,9 +856,9 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qvz-oU-TII">
<rect key="frame" x="317" y="371" width="99" height="18"/>
<rect key="frame" x="220" y="371" width="34" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title=" Command" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="1e6-xZ-tnC">
<buttonCell key="cell" type="check" title="" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="1e6-xZ-tnC">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -845,7 +867,7 @@
</connections>
</button>
<textField verticalHuggingPriority="750" fixedFrame="YES" textCompletion="NO" translatesAutoresizingMaskIntoConstraints="NO" id="F2s-m3-A6w" customClass="MyTextField">
<rect key="frame" x="420" y="367" width="60" height="25"/>
<rect key="frame" x="330" y="367" width="60" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" borderStyle="bezel" alignment="center" title="z" drawsBackground="YES" id="KXO-pT-dst">
<font key="font" metaFont="system" size="15"/>
Expand Down Expand Up @@ -884,7 +906,7 @@
</textFieldCell>
</textField>
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pSJ-2j-2yI">
<rect key="frame" x="111" y="404" width="128" height="25"/>
<rect key="frame" x="110" y="404" width="128" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="Telex" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="y7i-dK-uIA" id="fLB-yN-zN7">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
Expand Down Expand Up @@ -1059,7 +1081,7 @@
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qjz-9i-VOQ">
<rect key="frame" x="112" y="336" width="84" height="18"/>
<rect key="frame" x="110" y="336" width="84" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="radio" title="Tiếng Việt" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="qSg-7a-q1T">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand Down Expand Up @@ -1168,10 +1190,12 @@
</view>
<connections>
<outlet property="CheckSpellingButton" destination="6Ko-dG-wVl" id="GZc-qh-LkA"/>
<outlet property="CustomBeepSound" destination="bJy-t3-bzy" id="AyJ-En-XLs"/>
<outlet property="CustomSwitchCommand" destination="qvz-oU-TII" id="uv1-93-sB7"/>
<outlet property="CustomSwitchControl" destination="5e5-3S-Gd8" id="Zkp-PA-ngW"/>
<outlet property="CustomSwitchKey" destination="F2s-m3-A6w" id="xrK-un-OiP"/>
<outlet property="CustomSwitchOption" destination="Mox-UL-j9u" id="iCB-jD-eQG"/>
<outlet property="CustomSwitchShift" destination="wh2-uV-nSn" id="uJw-Tj-F5N"/>
<outlet property="EngButton" destination="yUA-UQ-mxx" id="QKJ-tU-dsA"/>
<outlet property="FreeMarkButton" destination="f8B-EV-EE1" id="u6V-zD-Xqg"/>
<outlet property="QuickTelex" destination="LNA-pk-XQM" id="Iso-f9-PzK"/>
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenKey/macOS/ModernKey/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.17</string>
<string>1.0.18</string>
<key>CFBundleVersion</key>
<string>17</string>
<string>18</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
7 changes: 6 additions & 1 deletion Sources/OpenKey/macOS/ModernKey/MyTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ - (BOOL)becomeFirstResponder {

if (!eventMonitor) {
eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event) {
if (event.keyCode == kVK_Space || event.keyCode == kVK_Delete || event.keyCode == kVK_ForwardDelete) {
if (event.keyCode == kVK_Space) {
[self setStringValue:@"Space"];
[self.Parent onSwitchKeyChange:kVK_Space character:kVK_Space];
} else if (event.keyCode == kVK_Delete || event.keyCode == kVK_ForwardDelete) {
[self setStringValue:@""];
[self.Parent onSwitchKeyChange:0xFE character:0xFE];
} else {
[self setStringValue:@""];
char t =event.characters.UTF8String[0];
Expand All @@ -51,6 +54,8 @@ -(void) textDidEndEditing:(NSNotification *)notification {
-(void)setTextByChar:(unsigned short)chr {
if (chr == kVK_Space) {
[self setStringValue:@"Space"];
} else if (chr == 0xFE) {
[self setStringValue:@""];
} else {
NSString* str = [NSString stringWithFormat:@"%c", chr];
[self setStringValue:str];
Expand Down
33 changes: 20 additions & 13 deletions Sources/OpenKey/macOS/ModernKey/OpenKey.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,19 @@ void SendBackspace() {
}
}

bool canSwitchKey() {
bool canSwitchKey(bool checkKeyCode=true) {
if (HAS_CONTROL(vSwitchKeyStatus) && !(_flag & kCGEventFlagMaskControl))
return false;
if (HAS_OPTION(vSwitchKeyStatus) && !(_flag & kCGEventFlagMaskAlternate))
return false;
if (HAS_COMMAND(vSwitchKeyStatus) && !(_flag & kCGEventFlagMaskCommand))
return false;
if (GET_SWITCH_KEY(vSwitchKeyStatus) != _keycode)
if (HAS_SHIFT(vSwitchKeyStatus) && !(_flag & kCGEventFlagMaskShift))
return false;
if (checkKeyCode) {
if (GET_SWITCH_KEY(vSwitchKeyStatus) != _keycode)
return false;
}
return true;
}

Expand All @@ -160,17 +164,20 @@ CGEventRef OpenKeyCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef e
_keycode = (CGKeyCode)CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

//switch language shortcut
if ((type == kCGEventKeyDown) && canSwitchKey()) {
if (vLanguage == 0)
vLanguage = 1;
else
vLanguage = 0;
NSBeep();
[appDelegate onInputMethodSelected];

startNewSession();

return NULL;
if (type == kCGEventKeyDown || type == kCGEventFlagsChanged) {
if (canSwitchKey(type != kCGEventFlagsChanged || GET_SWITCH_KEY(vSwitchKeyStatus) != 0xFE)) {
if (vLanguage == 0)
vLanguage = 1;
else
vLanguage = 0;
if (HAS_BEEP(vSwitchKeyStatus))
NSBeep();
[appDelegate onInputMethodSelected];

startNewSession();

return NULL;
}
}

if (vLanguage == 0) //ignore if is english
Expand Down
1 change: 1 addition & 0 deletions Sources/OpenKey/macOS/ModernKey/OpenKeyManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ +(BOOL)initEventTap {
// Create an event tap. We are interested in key presses.
eventMask = ((1 << kCGEventKeyDown) |
(1 << kCGEventKeyUp) |
(1 << kCGEventFlagsChanged) |
(1 << kCGEventLeftMouseDown) |
(1 << kCGEventRightMouseDown) |
(1 << kCGEventLeftMouseDragged) |
Expand Down
22 changes: 20 additions & 2 deletions Sources/OpenKey/macOS/ModernKey/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ @implementation ViewController {
__weak IBOutlet NSButton *CustomSwitchCommand;
__weak IBOutlet NSButton *CustomSwitchOption;
__weak IBOutlet NSButton *CustomSwitchControl;
__weak IBOutlet NSButton *CustomSwitchShift;
__weak IBOutlet MyTextField *CustomSwitchKey;
__weak IBOutlet NSButton *CustomBeepSound;
}

- (void)viewDidLoad {
Expand Down Expand Up @@ -156,11 +158,25 @@ - (IBAction)onCommandSwitchKey:(NSButton *)sender {
[[NSUserDefaults standardUserDefaults] setInteger:vSwitchKeyStatus forKey:@"SwitchKeyStatus"];
}

- (IBAction)onShiftSwitchKey:(NSButton *)sender {
NSInteger val = [self setCustomValue:sender keyToSet:nil];
vSwitchKeyStatus &= (~0x800);
vSwitchKeyStatus |= val << 11;
[[NSUserDefaults standardUserDefaults] setInteger:vSwitchKeyStatus forKey:@"SwitchKeyStatus"];
}

-(void)onSwitchKeyChange:(unsigned short)keyCode character:(unsigned short)ch {
vSwitchKeyStatus &= 0xFFFFFF00;
vSwitchKeyStatus |= (char)keyCode;
vSwitchKeyStatus |= keyCode;
vSwitchKeyStatus &= 0x00FFFFFF;
vSwitchKeyStatus |= ((int)ch<<24);
vSwitchKeyStatus |= ((unsigned int)ch<<24);
[[NSUserDefaults standardUserDefaults] setInteger:vSwitchKeyStatus forKey:@"SwitchKeyStatus"];
}

- (IBAction)onBeepSound:(NSButton *)sender {
unsigned int val = (unsigned int)[self setCustomValue:sender keyToSet:nil];
vSwitchKeyStatus &= (~0x8000);
vSwitchKeyStatus |= val << 15;
[[NSUserDefaults standardUserDefaults] setInteger:vSwitchKeyStatus forKey:@"SwitchKeyStatus"];
}

Expand Down Expand Up @@ -219,6 +235,8 @@ -(void)fillData {
CustomSwitchControl.state = (vSwitchKeyStatus & 0x100) ? NSControlStateValueOn : NSControlStateValueOff;
CustomSwitchOption.state = (vSwitchKeyStatus & 0x200) ? NSControlStateValueOn : NSControlStateValueOff;
CustomSwitchCommand.state = (vSwitchKeyStatus & 0x400) ? NSControlStateValueOn : NSControlStateValueOff;
CustomSwitchShift.state = (vSwitchKeyStatus & 0x800) ? NSControlStateValueOn : NSControlStateValueOff;
CustomBeepSound.state = (vSwitchKeyStatus & 0x8000) ? NSControlStateValueOn : NSControlStateValueOff;
[CustomSwitchKey setTextByChar:((vSwitchKeyStatus>>24) & 0xFF)];

}
Expand Down

0 comments on commit f50b894

Please sign in to comment.