How can I do a shortcut with the up and down arrow to change the value by one of the Edit numeric field?

4 次查看(过去 30 天)
I'm doing a GUI and I want to change the value with the keyboard by using this code and making a callback of WindowsKeyReleaseFnc but I also used the KeyPressFcn and know it changes double, and instead of changing just one field it changes every field selected with the KeyPressFcn and I dont know yet how can I remove it.
key = event.Key;
value = app.Ibias_SH.Value
if strcmp(key,'downarrow')
value=value-1;
if value<0
value=12
end
elseif strcmp(key,'uparrow')
value=value+1;
if value>12
value=0;
end
end
app.Ibias_SH.Value = value;
Thanks

回答(1 个)

Ravi
Ravi 2023-10-10
Hi IGNACIO SERRANO SANCHEZ-YBARGUEN,
If you want multiple EditField values to be changed when pressing the up and down arrows, create a keypress callback function in AppDesigner. Include all those EditField entries whose values are to be changed.
function UIFigureKeyPress(app, event)
key = event.Key;
value = app.CounterEditField.Value;
value1 = app.Counter1EditField.Value;
if ~strcmp(key, 'uparrow')
value = value - 1;
value1 = value1 - 1;
elseif ~strcmp(key, 'downarrow')
value = value + 1;
value1 = value1 + 1;
end
app.CounterEditField.Value = value;
app.Counter1EditField.Value = value1;
end
In the above code, the keypress callback is applied to two EditField values (CounterEditField, Counter1EditField). When an up arrow is pressed, the value in both the edit fields get incremented and when a down arrow is pressed, the value gets decremented.
If you want to change the value by 1 but specific to only one EditField at a given instance of time, then I suggest you use a Spinner component instead of an EditField. In a Spinner component, you can increment and decrement the value by clicking on the component in the app without explicitly creating any callback functions.
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by