将键盘快捷键发送到Mac OS X窗口 [英] Send a keyboard shortcut to a Mac OS X Window

查看:181
本文介绍了将键盘快捷键发送到Mac OS X窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac桌面上的一个窗口是否可以通过编程方式将键盘快捷方式或键序列发送给另一个窗口?

Is it possible for one window on a Mac desktop to programatically send a keyboard shortcut or key sequence to another?

我希望通过使用应用程序的键盘快捷键功能来控制不提供API的应用程序.

I'm looking to control an application which offers no API to do such, by using the application's keyboard shortcut features.

我相当确定这可以在Windows上完成,但在Mac上可以实现?

I'm fairly sure this can be done on Windows, but Mac?

谢谢

推荐答案

一种方法是将Applescript嵌入到Objective-C应用程序中. 例如,执行此Apple脚本,将 Command + M 发送到System Events应用程序:

One way to do this is embedding Applescript in your Objective-C application. For example executing this apple script, sends Command + M to System Events application:

tell application "System Events" to keystroke "m" using {command down}

您可以将上述脚本嵌入到Cocoa应用程序中,如下所示:

You can embed the script above in your Cocoa application with something like this:

//AppControler.h
#import <Cocoa/Cocoa.h>

@interface AppController : NSObject {
    NSAppleScript *key;
}
-(IBAction)sendkeys:(id)sender;
@end

//AppControler.m
#import "AppController.h"

@implementation AppController

-(IBAction)sendkeys:(id)sender
{
    NSAppleScript *key = [[NSAppleScript alloc] initWithSource:@"tell application "System Events" to keystroke "m" using {command down}"];
    [start executeAndReturnError:nil];
}

@end

这篇关于将键盘快捷键发送到Mac OS X窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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