mirror of
https://github.com/keycastr/keycastr.git
synced 2026-05-15 14:15:50 -06:00
Add Windows keystroke transformation support
Enhance KCEventTransformer to include a method for transforming keystrokes to their Windows equivalents. Update KCDefaultVisualizer to display both macOS and Windows keystroke representations when applicable.
This commit is contained in:
parent
58133a7bca
commit
9e760bf123
3 changed files with 63 additions and 1 deletions
|
|
@ -33,6 +33,7 @@
|
|||
#import "KCDefaultVisualizer.h"
|
||||
#import "KCKeystroke.h"
|
||||
#import "KCMouseEvent.h"
|
||||
#import "KCEventTransformer.h"
|
||||
#import "NSBezierPath+RoundedRect.h"
|
||||
#import "NSUserDefaults+Utility.h"
|
||||
|
||||
|
|
@ -316,7 +317,17 @@ static NSRect KC_defaultFrame(void) {
|
|||
[self abandonCurrentBezelView];
|
||||
}
|
||||
|
||||
[self appendString:[keystroke convertToString]];
|
||||
NSString *macString = [keystroke convertToString];
|
||||
NSString *winString = [[KCEventTransformer currentTransformer] transformedValueForWindows:keystroke];
|
||||
|
||||
NSString *displayString;
|
||||
if (winString.length > 0) {
|
||||
displayString = [NSString stringWithFormat:@"%@ | %@", macString, winString];
|
||||
} else {
|
||||
displayString = macString;
|
||||
}
|
||||
|
||||
[self appendString:displayString];
|
||||
}
|
||||
|
||||
- (void)appendString:(NSString *)string
|
||||
|
|
|
|||
|
|
@ -41,4 +41,6 @@
|
|||
|
||||
- (id)transformedValue:(KCKeycastrEvent *)event;
|
||||
|
||||
- (NSString *)transformedValueForWindows:(KCKeycastrEvent *)event;
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -322,6 +322,55 @@ static NSString* kLeftTabString = @"\xe2\x87\xa4";
|
|||
return mutableResponse;
|
||||
}
|
||||
|
||||
- (NSString *)transformedValueForWindows:(KCKeycastrEvent *)event {
|
||||
if (![event isKindOfClass:[KCKeystroke class]]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
KCKeystroke *keystroke = (KCKeystroke *)event;
|
||||
NSEventModifierFlags modifiers = event.modifierFlags;
|
||||
|
||||
BOOL isCommand = (modifiers & NSEventModifierFlagCommand) != 0;
|
||||
BOOL isControl = (modifiers & NSEventModifierFlagControl) != 0;
|
||||
BOOL isOption = (modifiers & NSEventModifierFlagOption) != 0;
|
||||
BOOL isShift = (modifiers & NSEventModifierFlagShift) != 0;
|
||||
|
||||
// Only show Windows equivalent if there are modifiers (Command, Control, or Option)
|
||||
// We treat Shift-only as a "simple letter" case usually, unless it's a special key?
|
||||
// User said: "don't duplicate simple letters like 'a | a'".
|
||||
// Shift+A -> "A". Windows: "Shift+A"? Or just "A"?
|
||||
// Usually Windows shortcuts are Ctrl+C.
|
||||
// Let's require Command, Control, or Option for now.
|
||||
if (!isCommand && !isControl && !isOption) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSMutableArray *parts = [NSMutableArray array];
|
||||
|
||||
if (isControl) [parts addObject:@"Win"];
|
||||
if (isCommand) [parts addObject:@"Ctrl"];
|
||||
if (isOption) [parts addObject:@"Alt"];
|
||||
if (isShift) [parts addObject:@"Shift"];
|
||||
|
||||
// Get the character key
|
||||
NSString *charString = nil;
|
||||
NSString *specialKeyString = [[self _specialKeys] objectForKey:@(keystroke.keyCode)];
|
||||
if (specialKeyString) {
|
||||
// We might want to map some symbols to text if possible, but keeping them as is for now is safer
|
||||
// unless we have a map for Windows names.
|
||||
// For example, arrow keys are symbols in _specialKeys.
|
||||
charString = specialKeyString;
|
||||
} else {
|
||||
charString = [self translatedCharacterForKeystroke:keystroke];
|
||||
}
|
||||
|
||||
if (charString.length > 0) {
|
||||
[parts addObject:[charString uppercaseString]];
|
||||
}
|
||||
|
||||
return [parts componentsJoinedByString:@"+"];
|
||||
}
|
||||
|
||||
- (NSString *)translatedCharacterForKeystroke:(KCKeystroke *)keystroke {
|
||||
if ([self shouldReturnOriginalCharactersForKeyCode:keystroke.keyCode
|
||||
characters:keystroke.characters] && keystroke.isCommand) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue