Home > Net >  How to change variable value based on keyboard input by using KeyPressFcn?
How to change variable value based on keyboard input by using KeyPressFcn?

Time:11-15

I'm trying to increase/decrease the value of a variable by using the right/left arrow key. The new value of the variable shall then be used in the next loop. To break the loop the return key can be used.

In order to realize this, I tried to build a function that contains the KeyPressFcn and a while-loop with an additional if-statement.

hkey = figure(1);                                                           
set(hkey,'KeyPressFcn',@UserFeedback);                                     
function UserFeedback(~,evnt)

    user_done = 0;                                                       % set confirmationstatus to 0 user has not yet chosen level
    intensitylevel = 1;                                                  % set start intensity = 1
     
    while user_done == 0                                                 % as long as user has not decided user can make changes

        waitforbuttonpress;                                              % wait for keyboard input


        if strcmpi(evnt.Key,'leftarrow')                                  % user presses left arrow key
            intensitylevel = intensitylevel-1;
            disp(intensitylevel)
        elseif strcmpi(evnt.Key,'rightarrow')                             % user presses right arrow key
            intensitylevel = intensitylevel 1;
            disp(intensitylevel)
        elseif strcmpi(evnt.Key,'return')                                 % user presses enter key
            intensitylevel = intensitylevel;
            user_done = 1;                                                % set condition to end the while loop
            disp(intensitylevel)
            break                                                         % end if loop
        end
    end
end

CodePudding user response:

Well, I guess the comments pretty much answer your question but just because I spent some time on it here is a full example. Really the bottom line is to get rid of the while loop and define intensitylevel either globally or as a parameter that is accessible from within the UserFeedback function.

update_color;

function update_color()
    hkey = figure;
    return_pressed = false;
    intensitylevel = 1;
    set(hkey,'KeyPressFcn',@(src, evnt) UserFeedback2(src, evnt));
    waitfor(hkey);
    
    function UserFeedback2(~,evnt)

        if return_pressed
            disp('Already done here. Changes not allowed!')
            return;
        end
        if strcmpi(evnt.Key,'leftarrow')                                  % user presses left arrow key
            intensitylevel = intensitylevel - 1;
            disp(num2str(intensitylevel))
            pause;
        elseif strcmpi(evnt.Key,'rightarrow')                             % user presses right arrow key
            intensitylevel = intensitylevel   1;
            disp(num2str(intensitylevel))
            pause;
        elseif strcmpi(evnt.Key,'return')                                 % user presses enter key
            return_pressed = true;
            disp(num2str(intensitylevel))
        end
    end
end
  • Related