|
Designed for computer science instruction, Turing is simply the easiest, most fun, and most effective way of teaching programming concepts. |
|
Quick Links |
||
The answer is fairly simple. Because you don't know whether you
are going to receive a keystroke on the keyboard or a click on
the mouse button, you can't just use getch or
buttonwait. Calling either of these procedures
would cause the program to (in the case of getch)
wait for a keystroke and ignore any mouse clicks or (in
the case of buttonwait) wait for a button click and
ignore any keystrokes
What you need to do is to check for keyboard input using hasch
before reading from the keyboard and check for a button click using
buttonmoved before reading the mouse stroke.
By doing so, you make sure that the program never halts waiting for a particular form of input until the input is there to be read. The other advantage to this method is that the program is never stuck waiting for any kind of input. Thus you can have graphics or music going on in the background while waiting for the user to enter input of some sort.
Here is an example showing how to get the next key or mouse action:
var ch : string (1)
var x, y, btnnum, btnupdn : int
loop
% Check if a key has been pressed
if hasch then
% A keystroke has been pressed. Read it.
getch (ch)
locate (1, 1)
put "The character entered is a: ", ch
end if
% Check if a mouse button has been pressed
if buttonmoved ("down") then
% A button has been pressed. Read it.
buttonwait ("down", x, y, btnnum, btnupdn)
locate (1, 1)
put "The button was clicked at: ", x, ", ",y
drawoval (x, y, 3, 3, 1)
end if
% Do some music or graphics here
...
end loop