1

以下に示すような単純な種類のGUIコードがあります。この「test_keypress」関数は図を作成し、キープレス(スペース)に応答します。

ここで、Matlabが特定の期間(たとえば1秒間)に1回のキー押下のみを受け入れるように制約を追加したいと思います。つまり、前回のキー押下から1秒以内に発生した場合、キー押下を拒否したいと思います。

どうやってやるの?

function test_keypress

f = figure;
set(f,'KeyPressFcn',@figInput);
imshow(ones(200,200,3));

end


function figInput(src,evnt)

if strcmp(evnt.Key,'space')
    imshow(rand(200,200,3));
end

end
4

1 に答える 1

3

現在の時刻を保存しimshow、最後の時刻から少なくとも100秒後にキーが押された場合にのみコマンドを評価できます。

function test_keypress

f = figure;
set(f,'KeyPressFcn',@figInput);
imshow(ones(200,200,3));

%# initialize time to "now"
set(f,'userData',clock);

end


function figInput(src,evnt)

currentTime = clock;
%# get last time
lastTime = get(src,'UserData');

%# show new figure if the right key has been pressed and at least
%# 100 seconds have elapsed
if strcmp(evnt.Key,'space') && etime(lastTime,currentTime) > 100
    imshow(rand(200,200,3));
    %# also update time
    set(src,'UserData',currentTime)
end

end
于 2012-12-10T16:42:23.273 に答える