简体   繁体   English

在Mac OS X中移动光标(在Python和/或R中)

[英]Moving the cursor in Mac OS X (in Python and/or R)

Can someone please help me figure out how I can use a Python or R script to move the cursor in Mac OS X? 有人可以帮我弄清楚如何使用Python或R脚本在Mac OS X中移动光标吗? The plan is to read a real time signal and based on the streamed value move the cursor up or down. 计划是读取实时信号,并根据流的值向上或向下移动光标。 Thanks for your help ... Thanks! 感谢您的帮助...谢谢!

Well, R allows you to use C so it smells like cheating, but works: 好吧,R允许您使用C,因此闻起来像作弊,但可以:

library(inline)
move.cursor <- cfunction(c(x="numeric",y="numeric"),
  "CGWarpMouseCursorPosition(CGPointMake(asReal(x),asReal(y)));
   return R_NilValue;",
  "#include <ApplicationServices/ApplicationServices.h>",,"C",
  libargs="-framework AppKit")

then you move the cursor simply by calling move.cursor : 然后只需调用move.cursor即可移动光标:

move.cursor(100, 100)

Also, in case someone wants to do the keyboard events, this is how it's done. 另外,如果有人要执行键盘事件,这就是这样做的方式。 Below is the codes for typing numbers: 以下是键入数字的代码:

which.key <- 1
eval(parse(text = paste("c.key <- 'CGEventRef e = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)", which.key+17, ", true); CGEventPost(kCGSessionEventTap, e); CFRelease(e); return R_NilValue;'", sep = "")))

KeyPress <- cfunction(
    body = c.key,
    includes = "#include <ApplicationServices/ApplicationServices.h>",
    language = "C",
    libargs = "-framework AppKit")

KeyPress()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM