Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to raise system-wide "keypress" events programmatically from within Mathematica?

Say "Ctrl+C" or "Ctrl+V", which all other programs can recognize?

This is an attempt to gain simple control/automation of other programs from within the Mathematica environment

share|improve this question
1  
I'm pretty sure that this cannot be done within Mathematica, as it is not a system-wide interface. But you can use the extremely easy-to-learn AutoHotkey which allows you to do exactly this. –  István Zachar Sep 25 '13 at 6:55
    
Thanks for the solution. I was using Autoit and it is a Windows-specific solution. Doing it using Mathematica would be cross-platform I thought –  my account_ram Sep 25 '13 at 7:30

1 Answer 1

One possibility is to use JLink

Needs["JLink`"]; 
ReinstallJava[]; 
LoadJavaClass["java.awt.event.KeyEvent"]; 
robotclass = JavaNew["java.awt.Robot"]; 
key = (Function[k,robotclass[keyPress[Symbol["KeyEvent`VKU"<>k]]]] /@ {##};
       Function[k,robotclass[keyRelease[Symbol["KeyEvent`VKU"<>k]]]] /@ {##}
       )&;
(* e.g., select all Input cells in this notebook :*)
NotebookFind[InputNotebook[], "Input", All, CellStyle]; 
(* and press and reslase Ctrl C programmatically *)
key["CONTROL", "C"]; 

(* create a new document *)
CreateDocument[TextCell["Robot says hi ", "Text"]];
(* use the robotclass to hit the down arrow key, thus moving below the cell *)
key["DOWN"];key["DOWN"];
(* and now ececut Ctrl V by robot: *)
key["CONTROL", "V"];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.