如何从我的全局热键中粘贴其他应用程序 [英] How to get other application to paste from my global hotkey

查看:112
本文介绍了如何从我的全局热键中粘贴其他应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小型的生产力工具,可以通过剪贴板进行一些字符串操作.

I have written a small productivity tool that does a few string manipulations via the clipboard.

当前正在注册一个热键,它会在其中拉出剪贴板中的文本,对其进行处理,然后将结果转储到剪贴板中.

It is currently registering a hot key, where it pulls in the clipboard text, processes it, and dumps the result back on the clipboard.

我已在CMD + SHIFT + V上安装了此

I have this installed on CMD+SHIFT+V

当前,您需要从另一个应用程序执行的操作是复制(CMD + C),然后激活我的热控器(CMD + SHIFT + V),然后必须使用(CMD + V)将其粘贴回原始应用程序中

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

如果可能的话,我想省去第三步,所以我的处理程序会以某种方式告知要粘贴的活动应用程序.

I'd like to eliminate the third step if possible, so my hothandler somehow tells whatever is the active application to paste.

任何建议如何做到这一点?

Any suggestions how to do this?

我的代码(减去无聊的文字替换的实际内容)是这样的:

My code (minus the actual boring text replacement stuff) is this:

请注意,热键处理程序需要碳框架

我从堆栈溢出时的此答案借来了热键处理程序代码.

I borrowed code from this answer on stack overflow 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

推荐答案

事实证明,有一种方法可以满足我的要求.

It 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);

}

这篇关于如何从我的全局热键中粘贴其他应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆