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.

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*)
share|improve this question

1 Answer 1

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:

 EventHandler[
  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:

 SetOptions[EvaluationNotebook[], 
  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 :> {}]
share|improve this answer
    
:I eveluated code what you are mentioned,still it's not working .If you don't mine can you explain clearly..... –  subbu Oct 15 '12 at 12:03
    
@subbu You'll have to be more clear in what code you evaluated and in what way it did not work. –  jVincent Oct 15 '12 at 12:06
    
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. –  subbu Oct 15 '12 at 12:13

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.