GUI Button with keyboard shortcut

조회 수: 27 (최근 30일)
Jan
Jan 2011년 7월 20일
Hi,
I have a button in a GUI. The user may press it to execute the Callback. Alternatively, I would like the user to be able to press a shortcut key (for example, 'c') to press the button instead of clicking to execute the same callback.
Any ideas how to do this?

채택된 답변

Jan
Jan 2011년 7월 27일
Thanks Walter. I basically went with your solution.
g = figure('KeyPressFcn', @keyPress)
MyButton = uicontrol('Style', 'pushbutton','Callback',@task);
function task(src, e)
disp('button press');
end
function keyPress(src, e)
switch e.Key
case 'c'
task(MyButton, []);
end
end

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 7월 20일
MyButton = uicontrol('Style','push','Callback',@whatever) set(gca, 'KeyPressFcn', @(src,event) CheckKeys(src,event,MyButton))
inside CheckKeys, if the data in the event structure shows you that the appropriate key has been pressed, then
cb = get(MyButton, 'CallBack');
cb{1}(src, [], cb{2:end})
The [] here is a fake event value, which is fine since Pushbuttons do not have a meaningful event value.
This setup presumes you used the function-handle form of setting the callback. If you used the string form of setting the callback... then Don't! that causes more trouble then it is worth.

Harry MacDowel
Harry MacDowel 2011년 7월 20일
Yes you can, Jan.
If you want to do that, you can define the KeyPressFcn by passing the input into a function you created yourself to do whatever you would like to.
I am lazy to post the whole procedure so I would give you the tutorial link on blinkdagger by Quan Quach here:-
Good luck!
  댓글 수: 1
Jan
Jan 2011년 7월 20일
Unfortunately, the tutorial only goes over how to use the KeyPressFcn property to initiate a callback, but it doesn't go over how to link a pushbutton to a keyboard shortcut. I'm not quite sure if there's a simple way to do this..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by