【问题标题】:The code for Simulating key press doesn't work, am I missing something here?模拟按键的代码不起作用,我在这里遗漏了什么吗?
【发布时间】:2021-10-24 08:58:23
【问题描述】:

我正在尝试模拟 cmd+v 来粘贴我复制的字符串。 我用下面的代码来模拟:

#define KEY_CODE_x ((CGKeyCode)7)
#define KEY_CODE_c ((CGKeyCode)8)
#define KEY_CODE_v ((CGKeyCode)9)

void PostCommandAndKey(CGKeyCode keyCode)
{    
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
    CGEventRef eventDown = CGEventCreateKeyboardEvent(source, keyCode, YES);
    CGEventRef eventUp = CGEventCreateKeyboardEvent(source, keyCode, NO);

    CGEventSetFlags(eventDown, kCGEventFlagMaskCommand);
    CGEventPost(kCGSessionEventTap, eventDown);
    CGEventPost(kCGSessionEventTap, eventUp);

    CFRelease(eventUp);
    CFRelease(eventDown);
    CFRelease(source);
}

我是这样调用上述方法的:

- (void) pasteClicked:(id)sender{
    
    NSButton *button = (NSButton *)sender;
    PasteItem *pi = self.pasteItems[button.tag];
    
    [self.pasteBoard setString:pi.text forType:NSPasteboardTypeString];
    
    NSString * str = [self.pasteBoard stringForType:NSPasteboardTypeString];

    NSLog(@"pasting %@", str);
    
    PostCommandAndKey(KEY_CODE_v);
}

光标位于 pasteClicked: 按钮所在的同一窗口中的 NSTextField 上,但我没有看到任何粘贴的内容。然而,正如我在日志中看到的那样,str 变量得到了正确的值。

我使用的是 OS 11.5.2。

我在这里缺少什么? 任何线索将不胜感激。

【问题讨论】:

  • 我都不知道你在什么类下写代码。
  • @ElTomato:所有代码都在AppDelegate中
  • 你试过在没有命令键的情况下模拟按键“v”吗?
  • @Willeke:是的,也没有任何反应
  • 这是权限问题吗?

标签: objective-c macos cocoa


【解决方案1】:

我无法让您的 -pasteClicked: 方法在我的系统上运行。下面的演示会将一个字符串复制到剪贴板,然后检索它并将其设置在一个文本字段中,这似乎是您想要完成的。要在 Xcode 中运行,请删除 main.m 源代码并将其替换为以下内容。同时删除预先提供的 AppDelegate 以避免重复符号。

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
 NSWindow *window;
 NSTextField *txtFld;
}
 -(void) pasteClicked: (id)sender;
 -(void) buildMenu;
 -(void) buildWnd;
@end

@implementation AppDelegate

#define KEY_CODE_x ((CGKeyCode)7)
#define KEY_CODE_c ((CGKeyCode)8)
#define KEY_CODE_v ((CGKeyCode)9)

void PostCommandAndKey(CGKeyCode keyCode) {
 NSLog(@"keyCode: %hu", keyCode);
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
    CGEventRef eventDown = CGEventCreateKeyboardEvent(source, keyCode, YES);
    CGEventRef eventUp = CGEventCreateKeyboardEvent(source, keyCode, NO);

    CGEventSetFlags(eventDown, kCGEventFlagMaskCommand);
    CGEventPost(kCGSessionEventTap, eventDown);
    CGEventPost(kCGSessionEventTap, eventUp);

    CFRelease(eventUp);
    CFRelease(eventDown);
    CFRelease(source);
}

- (void) pasteClicked:(id)sender{
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSString *strToCopy = @"My text to be copied to clipboard.";
[pb declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:nil];
BOOL copied = [pb setString:strToCopy forType:NSPasteboardTypeString];
NSLog (@"copied = %d", copied);
NSString *string = [pb stringForType:NSPasteboardTypeString];
[txtFld setStringValue:string];
NSLog(@"pasting \"%@\"",string);
PostCommandAndKey(KEY_CODE_v);
}

-(void) buildMenu {
// **** Menu Bar **** //
 NSMenu *menubar = [NSMenu new];
 [NSApp setMainMenu:menubar];
// **** App Menu **** //
 NSMenuItem *appMenuItem = [NSMenuItem new];
 NSMenu *appMenu = [NSMenu new];
 [appMenu addItemWithTitle: @"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
 [appMenuItem setSubmenu:appMenu];
 [menubar addItem:appMenuItem];
}

-(void) buildWnd {

 #define _wndW  500
 #define _wndH  150

 window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH ) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable backing: NSBackingStoreBuffered defer: NO];

 [window center];
 [window setTitle: @"Test window"];
 [window makeKeyAndOrderFront: nil];

// **** Text Field **** //
txtFld = [[NSTextField alloc] initWithFrame:NSMakeRect( 30, _wndH - 50, _wndW - 60, 20 )];
[txtFld setEditable:YES];
[[window contentView] addSubview:txtFld];

 // **** Paste Button **** //
 NSButton *pasteBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 60, _wndH - 100, 95, 30 )];
 [pasteBtn setBezelStyle:NSBezelStyleRounded ];
 [pasteBtn setTitle: @"Paste"];
 [pasteBtn setAction: @selector (pasteClicked:)];
 [[window contentView] addSubview: pasteBtn];

 // **** Quit btn **** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 5, 40, 40 )];
 [quitBtn setBezelStyle:NSBezelStyleCircular ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAutoresizingMask: NSViewMinXMargin];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
}

-(void) applicationWillFinishLaunching: (NSNotification *)notification {
 [self buildMenu];
 [self buildWnd];
}

-(void) applicationDidFinishLaunching: (NSNotification *)notification {
}
@end

int main () {
 NSApplication *application = [NSApplication sharedApplication];
 AppDelegate *appDelegate = [[AppDelegate alloc] init];
 [application setDelegate:appDelegate];
 [application run];
 return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-23
    • 2011-02-01
    • 1970-01-01
    • 2023-03-31
    • 2021-06-14
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多