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:
Nick Borovets 2026-02-19 16:20:10 +03:00
parent 58133a7bca
commit 9e760bf123
3 changed files with 63 additions and 1 deletions

View file

@ -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

View file

@ -41,4 +41,6 @@
- (id)transformedValue:(KCKeycastrEvent *)event;
- (NSString *)transformedValueForWindows:(KCKeycastrEvent *)event;
@end

View file

@ -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) {