1

Can someone please help me figure out how I can use a Python or R script to move the cursor in 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!

rezakhorshidi
  • 539
  • 1
  • 4
  • 10
  • possible duplicate of http://stackoverflow.com/questions/281133/controlling-the-mouse-from-python-in-os-x (You can the answer there) – thikonom Aug 26 '12 at 03:11
  • I would like to see if someone has a solution for R – rezakhorshidi Aug 26 '12 at 03:57
  • You could use the C code in the 'possible duplicate' and run that from R. Not a Mac person though, so don't ask me technicalities of compiling C on a Mac and linking R with Mac OS system libraries... – Spacedman Aug 26 '12 at 10:05
  • @RSK I voted to close because this question is a duplicate. However, I too think the R answer would be interesting. I suggest you clean up the question slightly (removing the Python references as you have a Python answer) to avoid this question being closed. Note I also upvoted the question as I would enjoy seeing an R solution. – Tyler Rinker Aug 26 '12 at 14:54

2 Answers2

6

Well, R allows you to use C so it smells like cheating, but works:

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(100, 100)
Simon Urbanek
  • 13,842
  • 45
  • 45
  • Simon, I need to create spacebar press events using the same approach. Do you have any solution for that? I tried some codes, but couldn't get it working ... – rezakhorshidi Aug 29 '12 at 20:48
  • This answer http://stackoverflow.com/questions/2379867/simulating-key-press-events-in-mac-os-x shows the C code necessary - you just don't need `CGEventSetFlags` and you'll need to find the code for spacebar. I didn't have time to test it, though... – Simon Urbanek Aug 31 '12 at 03:01
  • Simon, I wrote the R version of that code and it works fine, however, I can't find CGKeyCode for spacebar and arrow keys. Is there anyway I can have a list of them and find them out? – rezakhorshidi Sep 05 '12 at 16:22
  • Done! For number keys: 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 ", language = "C", libargs = "-framework AppKit") KeyPress() – rezakhorshidi Sep 06 '12 at 04:53
0

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()
rezakhorshidi
  • 539
  • 1
  • 4
  • 10
  • You copied Simon's answer but forgot to include the library call? Come on... at least go all the way. – Dason Sep 07 '12 at 14:51