Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is <shift> key pressed ?

Subject: Is <shift> key pressed ?

From: Bill

Date: 29 Jul, 2008 13:36:02

Message: 1 of 9

Hi,

I have a gui with, among other things, buttons and edit
boxes. In several places these buttons are in pairs next
to an edit box, and are used to increment or decrement a
number in that edit box.

When the user clicks on one of these buttons the number in
the edit box should increase or decrease by, say, 1. What
I would like to do is find out whether the <shift> key is
also pressed - in which case the number should increase or
decrease by 10 instead.

I there any way I can detemine the <shift> key status
within the button callback function ?

Thanks,
Bill

Subject: Is <shift> key pressed ?

From: Rodney Thomson

Date: 29 Jul, 2008 14:08:01

Message: 2 of 9

"Bill " <reply2bill@live.com> wrote in message
<g6n6c2$6bl$1@fred.mathworks.com>...
> Hi,
>
> I have a gui with, among other things, buttons and edit
> boxes. In several places these buttons are in pairs next
> to an edit box, and are used to increment or decrement a
> number in that edit box.
>
> When the user clicks on one of these buttons the number in
> the edit box should increase or decrease by, say, 1. What
> I would like to do is find out whether the <shift> key is
> also pressed - in which case the number should increase or
> decrease by 10 instead.
>
> I there any way I can detemine the <shift> key status
> within the button callback function ?
>
> Thanks,
> Bill

You can do "get(handles.figure1, 'CurrentCharacter')" where
figure1 is your root figure. This will return the last
character pressed with the GUI in focus.

Unfortunately it does not seem to respond to Shift presses.

I would probably recommend using two push buttons, one up on
down to do what you require.

Rod

--
http://iheartmatlab.blogspot.com

Subject: Is <shift> key pressed ?

From: matt dash

Date: 29 Jul, 2008 16:04:04

Message: 3 of 9

"Rodney Thomson" <readmore@iheartmatlab.blogspot.com> wrote
in message <g6n881$shr$1@fred.mathworks.com>...
> "Bill " <reply2bill@live.com> wrote in message
> <g6n6c2$6bl$1@fred.mathworks.com>...
> > Hi,
> >
> > I have a gui with, among other things, buttons and edit
> > boxes. In several places these buttons are in pairs next
> > to an edit box, and are used to increment or decrement a
> > number in that edit box.
> >
> > When the user clicks on one of these buttons the number in
> > the edit box should increase or decrease by, say, 1. What
> > I would like to do is find out whether the <shift> key is
> > also pressed - in which case the number should increase or
> > decrease by 10 instead.
> >
> > I there any way I can detemine the <shift> key status
> > within the button callback function ?
> >
> > Thanks,
> > Bill
>
> You can do "get(handles.figure1, 'CurrentCharacter')" where
> figure1 is your root figure. This will return the last
> character pressed with the GUI in focus.
>
> Unfortunately it does not seem to respond to Shift presses.
>
> I would probably recommend using two push buttons, one up on
> down to do what you require.
>
> Rod
>
> --
> http://iheartmatlab.blogspot.com



You want the figure property "selectiontype" which will ==
'extend' if shift is currently being pressed.

Subject: Is <shift> key pressed ?

From: Bill

Date: 29 Jul, 2008 17:16:02

Message: 4 of 9

"matt dash" <n.a@mail.com> wrote in message
<g6nf1k$sfa$1@fred.mathworks.com>...
>
> You want the figure property "selectiontype" which will ==
> 'extend' if shift is currently being pressed.

Thanks both for your thooughts on this.

SelectionType gives exactly the information I'm looking
for - whether the Shift (or Ctrl) key is pressed. This is
(was) very encouraging.

Unfortunately, on trying it out, it only seems to be
effective in the callback function of my figure. If I try
to use it in the callback of a pushbutton, I always get
SelectionType==Normal - presumably because the focus is not
with the figure at that stage. It's a pity that
pushbuttons don't have a SelectionType property.

I wonder if there's any way around this?

Bill

Subject: Is <shift> key pressed ?

From: matt dash

Date: 29 Jul, 2008 18:15:03

Message: 5 of 9

"Bill " <reply2bill@live.com> wrote in message
<g6nj8i$jjv$1@fred.mathworks.com>...
> "matt dash" <n.a@mail.com> wrote in message
> <g6nf1k$sfa$1@fred.mathworks.com>...
> >
> > You want the figure property "selectiontype" which will ==
> > 'extend' if shift is currently being pressed.
>
> Thanks both for your thooughts on this.
>
> SelectionType gives exactly the information I'm looking
> for - whether the Shift (or Ctrl) key is pressed. This is
> (was) very encouraging.
>
> Unfortunately, on trying it out, it only seems to be
> effective in the callback function of my figure. If I try
> to use it in the callback of a pushbutton, I always get
> SelectionType==Normal - presumably because the focus is not
> with the figure at that stage. It's a pity that
> pushbuttons don't have a SelectionType property.
>
> I wonder if there's any way around this?
>
> Bill
>
>

huh. I never noticed that. Well one option is to use the
"buttondownfcn" instead of the "callback", but to do that
you need to set "enable" to "inactive", which means it won't
actually animate the pushing of the button. But it will let
you check the selection type property. Theres probaby a
better answer that I'm not thinking of...

Subject: Is <shift> key pressed ?

From: Hans Gohle

Date: 28 Aug, 2009 14:24:01

Message: 6 of 9

"matt dash" <n.a@mail.com> wrote in message <g6nmn7$527$1@fred.mathworks.com>...
> "Bill " <reply2bill@live.com> wrote in message
> <g6nj8i$jjv$1@fred.mathworks.com>...
> > "matt dash" <n.a@mail.com> wrote in message
> > <g6nf1k$sfa$1@fred.mathworks.com>...
> > >
> > > You want the figure property "selectiontype" which will ==
> > > 'extend' if shift is currently being pressed.
> >
> > Thanks both for your thooughts on this.
> >
> > SelectionType gives exactly the information I'm looking
> > for - whether the Shift (or Ctrl) key is pressed. This is
> > (was) very encouraging.
> >
> > Unfortunately, on trying it out, it only seems to be
> > effective in the callback function of my figure. If I try
> > to use it in the callback of a pushbutton, I always get
> > SelectionType==Normal - presumably because the focus is not
> > with the figure at that stage. It's a pity that
> > pushbuttons don't have a SelectionType property.
> >
> > I wonder if there's any way around this?

Hi,

I have the same problem as you described some time ago. Did you find a proper solution for that? Can you please let me know?

Thanks,
g.

Subject: Is <shift> key pressed ?

From: Andy

Date: 28 Aug, 2009 14:50:22

Message: 7 of 9

% Here's a less than ideal solution:

function testgui
    f=figure;

    ed=uicontrol('Parent',f,...
        'Units','normalized',...
        'Position',[0.3 0.45 0.2 0.1],...
        'String','0',...
        'BackgroundColor','white');

    pb=uicontrol('Parent',f,...
        'Units','normalized',...
        'Position',[0.3 0.3 0.2 0.1],...
        'String','Inc by 1',...
        'ToolTip','Right-click to change increment amount',...
        'ButtonDownFcn',{@pb_ButtonDownFcn},...
        'Callback',{@pb_Callback,1});
    
    function pb_Callback(src,eventdata,varargin)
        
        % varargin is amount to increment by
        str=get(ed,'String');
        num=str2double(str);
        newnum=num+varargin{1};
        set(ed,'String',num2str(newnum));
    end

    function pb_ButtonDownFcn(src,eventdata)
        
        str=get(pb,'String');
        
        if strcmp(str,'Inc by 1');
            set(pb,'String','Inc by 10');
            set(pb,'Callback',{@pb_Callback,10});
        else
            set(pb,'String','Inc by 1');
            set(pb,'Callback',{@pb_Callback,1});
        end
    end

end

% This could be expanded by adding a context menu with several increment values
% (1, 10, 100, etc), where the callback resets the string and callback properties
% of the pushbutton. But this doesn't catch <Shift> like you wanted.

Subject: Is <shift> key pressed ?

From: Yair Altman

Date: 29 Aug, 2009 20:15:20

Message: 8 of 9

You can use Java callbacks to determine all the relevant information about the click event (in the eventData object that is passed as a second argument). See more info here: http://undocumentedmatlab.com/blog/uicontrol-callbacks/

I also suggest using the standard Java Swing control JSpinner, as shown here: http://www.mathworks.com/matlabcentral/fileexchange/14583

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Is <shift> key pressed ?

From: Tobias

Date: 10 Mar, 2013 14:07:08

Message: 9 of 9

"Bill" wrote in message <g6n6c2$6bl$1@fred.mathworks.com>...
> Hi,
>
> I have a gui with, among other things, buttons and edit
> boxes. In several places these buttons are in pairs next
> to an edit box, and are used to increment or decrement a
> number in that edit box.
>
> When the user clicks on one of these buttons the number in
> the edit box should increase or decrease by, say, 1. What
> I would like to do is find out whether the <shift> key is
> also pressed - in which case the number should increase or
> decrease by 10 instead.
>
> I there any way I can detemine the <shift> key status
> within the button callback function ?
>
> Thanks,
> Bill

Also its been a while since Bill has asked the question here is a solution that works realy well:
modifiers = get(gcf,'currentModifier'); %(Use an actual figure number if known)
ctrlIsPressed = ismember('control',modifiers);

(I found this answer on stackoverflow.com. Thanks to Pursuit for posting this solution.)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us