Clear Filters
Clear Filters

F Keys as accelerators, not allowed!

6 views (last 30 days)
Antony
Antony on 8 Feb 2024
Commented: Antony on 14 Feb 2024
I'm using app designer, when I try to add the 'F5' key as an accelerator to a menu item,
app design gives me the message:
Its seems odd that function keys would not be allowed since they are amungst the most common short cuts.
What am I missing here.
Is there a way to make this work.

Accepted Answer

Ayush Singh
Ayush Singh on 14 Feb 2024
Hi Antony
According to my comprehension, you are trying to add 'F5' key as an accelerator to a menu item but getting an error on doing so.
In MATLAB App Designer, when you try to set an accelerator key for a menu item, the accelerator property typically expects a single character (such as 'N' for a "New" command) or an empty character array.
This is why you are seeing the error message when you try to set 'F5' as the accelerator, as 'F5' is more than one character and is not recognized as a valid input for the property.
However, there is a way to work around this limitation by using the 'KeyPressFcn' callback of the figure (the main UI window) to detect when the F5 key is pressed.
As a workaround try out the following:
% Assuming 'app' is the handle to your application
app.UIFigure.KeyPressFcn = @app.figureKeyPress;
% Now define the callback function within your app
% Callback function that gets executed on key press in the figure
function figureKeyPress(app, event)
% Check if the pressed key is F5
if strcmp(event.Key, 'f5')
% Call the function or execute the code associated with the menu item
app.menuItemCallback();
end
end
% The callback function for your menu item
function menuItemCallback(app)
% Code that the menu item would execute
end
  1. We set the 'KeyPressFcn' property of the figure to a custom callback function that you will define.
  2. In the callback function, check if the pressed key is 'f5'.
  3. If 'f5' is detected, execute the same code you would have tied to the menu item's accelerator.
I hope the above workaround resolves your query.
  1 Comment
Antony
Antony on 14 Feb 2024
Many thanks for your answer and code, this works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!