Cómo obtener otra aplicación para pegar desde mi tecla de acceso directo global
He escrito una pequeña herramienta de productividad que realiza algunas manipulaciones de cadenas a través del portapapeles.
Actualmente está registrando una tecla de acceso rápido, donde extrae el texto del portapapeles, lo procesa y vuelca el resultado en el portapapeles.
Tengo esto instalado en CMD + MAYÚS + V
actualmente, lo que debe hacer de otra aplicación es copiar (CMD + C) y luego activar mi hothandler (CMD + MAYÚS + V), y luego debe volver a pegarlo en la aplicación original con (CMD + V).
Me gustaría eliminar el tercer paso si es posible, por lo que mi manejador de alguna manera le dice a la aplicación activa que debe pegar.
¿Alguna sugerencia de cómo hacer esto?
Mi código (menos el material de reemplazo de texto aburrido real) es este:
Tenga en cuenta que esto necesita marco de carbono para los manejadores de teclas de acceso rápido
[EDIT:] Tomé prestado el código deesta respuesta en el desbordamiento de pila para el código del manejador de teclas de acceso rápido.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
EventHotKeyRef hotKeyRef;
}
@property (assign) IBOutlet NSWindow *window;
-(IBAction) checkClipboard:(id) sender ;
@end
AppDelegate.m
#import "AppDelegate.h"
//#import "NSString+cppMacros.h" // not relevant to question
OSStatus _AppDelegateHotKeyHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) {
AppDelegate *appDel = [[NSApplication sharedApplication] delegate];
[appDel checkClipboard:nil];
return noErr;
}
@implementation AppDelegate
- (void)installHotkey {
if (!hotKeyRef) {
EventHotKeyID hotKeyId;
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&_AppDelegateHotKeyHandler, 1, &eventType, NULL, NULL);
hotKeyId.signature = 'hotk';
hotKeyId.id = 1337;
RegisterEventHotKey(kVK_ANSI_V, cmdKey + shiftKey, hotKeyId, GetApplicationEventTarget(), 0, &hotKeyRef);
NSLog(@"_AppDelegateHotKeyHandler installed");
}
}
-(void) uninstallHotkey {
if (hotKeyRef) {
UnregisterEventHotKey(hotKeyRef);
hotKeyRef = nil;
NSLog(@"_AppDelegateHotKeyHandler uninstalled");
}
}
-(IBAction) checkClipboard:(id) sender {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
NSString *text = [pasteboard stringForType:NSPasteboardTypeString];
NSLog(@"clipboard input:%@",text);
/* actual code is this: (not relevant to question)
NSString *newText = [text isMacroEncoded] ? [text macroDecodedString] : [text macroEncodedString];
*/
// demo code for question
NSString *newText = [@"Pasted:" stringByAppendingString:text];
[pasteboard declareTypes:types owner:self];
[pasteboard setString:newText forType:NSStringPboardType];
NSLog(@"clipboard output:%@",newText);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
hotKeyRef = nil;
[self installHotkey];
}
-(void) applicationWillTerminate:(NSNotification *)notification {
[self uninstallHotkey];
}
@end