OSX将鼠标左键分配给键盘键 [英] OSX assign left mouse click to a keyboard key
本文介绍了OSX将鼠标左键分配给键盘键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够在键盘上分配一个按键,相当于单击鼠标左键.
I would like to be able to assign a key on my keyboard to be equivalent to a left mouse click.
理想情况下,它需要采取以下操作:按住键也等同于按住鼠标左键.
Ideally it needs to act such that holding the key down is also equivalent to holding the left mouse button down.
我希望以用户身份拥有此功能,此外,编程解决方案(可可/苹果脚本等)也将很棒.
I'd like this capability as a user, additionally a programmatic solution (cocoa/applescript etc) would be great too.
推荐答案
这可以通过编写一些代码来完成:
This can be done by writing some code:
编写一个全局处理程序以接收您要观看的事件的类型
Write a global handler to receive the type of event you want to watch
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask
handler:^(NSEvent *event){
NSLog(@"%i", [event keyCode]);
//todo invoke mouse clicking code;
}];
然后编写鼠标单击代码:
Then write the mouse click code:
// get current mouse pos
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
NSLog(@"Location? x= %f, y = %f", (float)point.x, (float)point.y);
// perform a click
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef theEvent = CGEventCreateMouseEvent(source, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventSetType(theEvent, kCGEventLeftMouseDown);
CGEventPost(kCGHIDEventTap, theEvent);
CFRelease(theEvent);
这篇关于OSX将鼠标左键分配给键盘键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文