How do you clear something like eventdata.Key so that I can run the function again?

I'm making a program that requires the user to type a word, hit enter and then compare the word to the one on the screen but I need to know how to clear the eventdata.Key so that the user can use the code I have written again when the next word pops up.
Here is what I have so far:
%create explanation message
uicontrol('style', 'text', 'units', 'normalized', 'position', [ .33 .1 .33 .05], 'string', 'Enter your responce below:');
%Position score counter
score_h = uicontrol('style', 'text', 'units', 'normalized', 'position', [.1 0 .1 .1], 'string', '0');
%Score text
uicontrol('style', 'text', 'units', 'normalized', 'position', [.1 .1 .1 .05], 'string', 'Score:');
%Create message area and word display area
word_h = uicontrol('style', 'text', 'units', 'normalized', 'position', [.35 .5 .3 .2],...
'string', 'Welcome to Speed Typer! Type the words as quickly as you can as they pop up and hit enter! Type ''start'' and hit enter to begin.');
%Create level display and label
uicontrol('style', 'text', 'units', 'normalized', 'position', [.8 .1 .1 .05], 'string', 'Level:')
level_h = uicontrol('style', 'text', 'units', 'normalized', 'position', [.8 0 .1 .1], 'string', 'N/A');
%Create typing area to start game then remove when done being used
textbox_hstart = uicontrol('style', 'edit', 'units', 'normalized', 'position', ...
[.33 .04 .33 .04], 'keypressfcn', {@keypress, word_h, level_h, score_h,...
beginner,easy, medium, hard, expert});
Here is the function it keeps skipping through the words because it's reading the eventdata.Key as always being tru:
function keypress(hObject, eventdata, word_h, level_h, score_h, beginner, easy, medium, hard, expert)
%Determine if the return key ws pressed, and check if they enters 'start'
counter = 0;
word = 'start';
game_on = 0;
if strcmp(eventdata.Key, 'return') == 1
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
robot.keyPress(KeyEvent.VK_ENTER);
pause(0.01)
robot.keyRelease(KeyEvent.VK_ENTER);
if strcmp(get(hObject, 'string'), 'start') == 1
word = beginner{randi([1,20])};
set(word_h, 'string', word);
set(level_h, 'string', 'Beginner')
%Start timer
gamestart = 1;
end
if gamestart == 1
for k = 1:50
if strcmp(eventdata.Key, 'return') == 1
import java.awt.Robot;
import java.awt.event.KeyEvent;
robot=Robot;
robot.keyPress(KeyEvent.VK_ENTER);
pause(0.01)
robot.keyRelease(KeyEvent.VK_ENTER);
if strcmp(get(hObject, 'string'), word) == 1
set(score_h, 'string', num2str(str2double(get(score_h, 'string'))+1));
counter = counter+1;
if counter <= 10
set(level_h, 'string', 'Beginner');
word = beginner{randi([1,20])};
set(word_h, 'string', word);
k = k+1;
elseif counter > 10 || counter <= 20
set(level_h, 'string', 'Easy');
word = easy{randi([1,20])};
set(word_h, 'string', word);
k = k+1;
elseif counter > 20 || counter <= 30
set(level_h, 'string', 'Medium');
word = medium{randi([1,20])};
set(word_h, 'string', word);
k = k+1;
elseif counter > 30 || counter <= 40
set(level_h, 'string', 'Hard');
word = hard{randi([1,20])};
set(word_h, 'string', word);
k = k+1;
elseif counter > 40
set(level_h, 'string', 'Expert');
word = expert{randi([1,20])};
set(word_h, 'string', word);
k = k+1;
end
elseif strcmp(get(hObject, 'string'), word) == 0
set(level_h, 'string', 'N/A');
%Stop timer
%calculate score
k = 50;
gamestart = 0;
end
end
end
end
%determine if key pressed was enter
%compare string in text box to 'start'
%If string matches:
%change word on screen
%change level
%determine if enter was pressed
%compare string on screen to users string
%if string is the same:
%add to score
%check if level needs to be updated (add to counter if not)
%display new word
%repeat
%if string is different:
%change level back to N/A
%stop timer
%calculate and display score
end

Antworten (0)

Gefragt:

am 25 Apr. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by