I want to connect a shortcut key (ctrl + c) to a Button in my application. To that purpose I wrote some code using EventHandler. When the mouse button is clicked it's working fine, but the ctrl + c key press isn't working.

 EventHandler[ Button["Click"],
     {"KeyDown", "c"} :>MessageDialog["This is button"],
      "MouseClicked" :>MessageDialog["This is button"]
 ](*EventHandler Function is closed*)

The definitions you give to EventHandler are only active when your selection is on its contents. An example of where the button press also works would be:

  InputField["Click"], {{"KeyDown", "c"} :> MessageDialog["This is button"], 
       "MouseClicked" :> MessageDialog["This is button"]}, 
 PassEventsDown -> True]

Where you will get the pop-up first when you press the field, and afterwards if you press c while the selection is in the input field. Notice I added PassEventsDown otherwise your cursor will not go into the field when you press it the first time.

If you want to have a global event that triggers when you press c, independent of where the selection is, you can add it to the NotebookEventActions of the notebook:

  NotebookEventActions :> {{"KeyDown", "c"} :> MessageDialog["This is button"]}]

Then any press of c will trigger the event. And if you need to get back (because it gets annoying having a pop-up any time you press c for instance) you simply set the NotebookEventActions back to {}

    SetOptions[EvaluationNotebook[], NotebookEventActions :> {}]
| improve this answer | |
  • $\begingroup$ :I eveluated code what you are mentioned,still it's not working .If you don't mine can you explain clearly..... $\endgroup$ – subbu Oct 15 '12 at 12:03
  • $\begingroup$ @subbu You'll have to be more clear in what code you evaluated and in what way it did not work. $\endgroup$ – jVincent Oct 15 '12 at 12:06
  • $\begingroup$ Actually I make a cdf with a singleButton,if I press (ctrl + c ) after open that cdf file,that coresponding Buttonaction will be perform ,I want like that. $\endgroup$ – subbu Oct 15 '12 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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