2012-12-10 3 views
1

아래와 같은 간단한 GUI 코드가 있습니다. 이 "test_keypress"기능은 그림을 생성하고 키 누르기 (공백)에 응답합니다.Matlab에서 특정 기간 동안 하나의 키 누르기 만 허용

이제 제약 조건을 추가하여 Matlab이 특정 기간 (예 : 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 

답변

3

당신은 현재 시간을 저장하고 키를 눌러 마지막 후 최소 100 초 발생한 경우에만 imshow 명령을 평가할 수 있습니다.

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 
관련 문제