Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Detect when key up?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Griffon2-6




PostPosted: Mon Jan 21, 2008 5:32 pm   Post subject: Detect when key up?

Hi,

Right now, I am doing a sound program for Turing that simulates a violin. Now I want a sound file to play when a key is pressed down, and while it is held down, I do not want the sound to autorepeat with the keystrokes. When the key is released, I want the sound to stop playing.

The thing I can't get working is the autorepeat part. I am stumped as to what I can do to prevent the sound file from autorepeating. I am using Input.KeyDown to detect keystrokes.

Can anyone help?

Thanks!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Jan 21, 2008 6:16 pm   Post subject: RE:Detect when key up?

Ask yourself what "on key up" event really means. In simple terms, it's when the key used to be down last time you checked it, but now it is up. Input.KeyDown tells you where they keys are now. Before calling it though, record the state of how the keys used to be before, then compare for changes.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ericfourfour




PostPosted: Mon Jan 21, 2008 11:44 pm   Post subject: Re: Detect when key up?

This is the simplest way of doing this is:
Turing:
if hasch and getchar = KEY_ENTER then
    % Play sound
    Input.Flush
end if


hasch returns true if there is a key down.
getchar returns the key that is down.
This combination is good if only one button is being used at a time.

Input.Flush empties the keyboard buffer (the memory that stores keystrokes).
Tony




PostPosted: Mon Jan 21, 2008 11:58 pm   Post subject: RE:Detect when key up?

But a pressed down key will continuously fill the buffer, so even if it's flushed at one point, it will just refill, and the code will execute again, from the same key.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ericfourfour




PostPosted: Tue Jan 22, 2008 3:48 pm   Post subject: RE:Detect when key up?

I think the question needs to be cleared up. I thought that he wanted it to play as long as the key is down and when the key is released, for it to stop playing. Griffon2-6 should post some code.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: