10

I need to know whether the user is holding down the ctrl key while clicking a button. Since it's a button and not a figure I cannot use 'selectionType' on the figure etc.

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
codekitty
  • 1,438
  • 3
  • 20
  • 30

2 Answers2

10

How about this:

modifiers = get(gcf,'currentModifier');        %(Use an actual figure number if known)
ctrlIsPressed = ismember('control',modifiers);

The figure class has a number of useful Current* properties which are useful when handling callbacks. This is how to retrieve current mouse position, selected graphics object, and (as here) pressed keys. These include: CurrentAxes, CurrentCharacter, CurrentKey, CurrentModifier, CurrentObject, and CurrentPosition.

Pursuit
  • 12,285
  • 1
  • 25
  • 41
  • It seems that the cmd modifier sticks around way after the key has been released. Is there any way to find out which keys are currently being pressed? – codekitty Jan 21 '15 at 23:30
1

Pressing the escape key reinitializes CurrentModifier. My solution so far has been to instruct my users (right in the GUI) to press the escape key to revert to default behavior.

Overall, Matlab's CurrentModifier behavior seems to be that the modifier key "sticks" until one of the following occurs: a different modifier is pressed, a different window is selected, or the escape key is pressed.

dfergenson
  • 31
  • 4