Create shortcut to pushbutton

조회 수: 6 (최근 30일)
zeyneb khalili
zeyneb khalili 2017년 5월 4일
답변: Divyajyoti Nayak 2024년 11월 17일
How can I create shortcuts (ctrl+letter) to my pushbutton?

답변 (1개)

Divyajyoti Nayak
Divyajyoti Nayak 2024년 11월 17일
To create a shortcut for push button in MATLB figures, the ‘KeyPressFcn’ and ‘KeyReleaseFcn’ callbacks of the figure object can be used.
Here are the steps I followed to program shortcut keys to a push button:
  • Setup the GUI figure and button.
f = figure;
button = uicontrol(f,'Style','pushbutton','String','Push','Callback',@buttonPressed);
  • Initialize a ‘ctrlPressed’ variable in the figure’s ‘UserData to store a value based on whether the Ctrl button is pressed or not.
f.UserData = struct('ctrlPressed',false);
  • Add the ‘KeyPressFcn’ and ‘KeyReleaseFcn’ callbacks to the figure.
f.KeyPressFcn = {@keyPressed, f, button};
f.KeyReleaseFcn = {@keyReleased, f};
  • Define the ‘KeyPressFcn’ callback to handle key press events.
function keyPressed (src, event, figureHandle, buttonHandle)
  • If Ctrl key is pressed, set ‘ctrlPressed’ to true.
switch event.Key
case 'control'
figureHandle.UserData.ctrlPressed = true;
  • If the desired shortcut key is pressed, call the callback function of the push button (Additional cases can be made for more shortcuts).
case 'd'
if(figureHandle.UserData.ctrlPressed == true)
buttonHandle.Callback();
end
end
end
  • Define the ‘KeyReleaseFcn’ to set ‘ctrlPressed’ to false if Ctrl button is released.
function keyReleased(src, event, figureHandle)
if(event.Key == 'control')
figureHandle.UserData.ctrlPressed = false;
end
end
  • Define button callback.
function buttonPressed(src,event)
disp('Button pressed');
end
For more information about the properties of the figure object used in the code, here are some documentations:

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by