How to get another paste application from my global hotkey

I wrote a small productivity tool that does a few string manipulations through the clipboard.

He currently registers a hotkey where he extracts the text of the clipboard, processes it and returns the result to the clipboard.

I have it installed on CMD + SHIFT + V

currently what you need to do from another application is a copy (CMD + C) and then activate my hothandler (CMD + SHIFT + V) and then you have to paste it back into the original application with (CMD + V) .

I would like to eliminate the third step, if possible, so my hothandler somehow reports that it is an active paste application.

Any suggestions on how to do this?

My code (minus the actual material to replace the text):

Please note: this requires carbon frames for keyboard shortcuts

[EDIT:] I used the code for this answer when overflowing the stack for the hotkey handler code.

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 
0
source share
2 answers

Turns out there is a way to do what I want.

 void pasteCurrent() { CGEventRef commandDown = CGEventCreateKeyboardEvent(NULL, kVK_Command, YES); CGEventRef VDown = CGEventCreateKeyboardEvent(NULL, kVK_ANSI_V, YES); CGEventRef VUp = CGEventCreateKeyboardEvent(NULL, kVK_ANSI_V, NO); CGEventRef commandUp = CGEventCreateKeyboardEvent(NULL, kVK_Command, NO); CGEventSetFlags(VDown,kCGEventFlagMaskCommand); CGEventSetFlags(VUp,kCGEventFlagMaskCommand); CGEventPost(kCGHIDEventTap, commandDown); CGEventPost(kCGHIDEventTap, VDown); CGEventPost(kCGHIDEventTap, VUp); CGEventPost(kCGHIDEventTap, commandUp); CFRelease(commandDown); CFRelease(VDown); CFRelease(VUp); CFRelease(commandUp); } 
+1
source

You do not want to use the global hotkey for this. You want to write your application so that it provides a β€œservice”. See Service Implementation Guide .

Your service can be activated using a keyboard shortcut, either by default, configured in your Info.plist application, or configured by the user in System Preferences.

Cocoa performs both copying from the active application and pasting into it after you have done the conversion, so this will take care of the first and third steps.

Services use a separate cardboard for data transfer, so this also does not interfere with the contents of a common (copy and paste) cardboard.

This is an excellent approach at every level.


Edited to add other benefits:

The service will not steal the keyboard shortcut from the active application.

The service is also available through the application menu and context menu. This makes it affordable.

+4
source

Source: https://habr.com/ru/post/987458/


All Articles