Turing Logo  

Designed for computer science instruction, Turing is simply the easiest, most fun, and most effective way of teaching programming concepts.


Quick Links
Home page of Holt Software Associates  | Home page of the Turing Programming Language, the fastest way to teach programming concepts  | Home page of Holt Software's Java products  | Home page of Ready to Program with Java(tm) Technology, a Java development environment designed for education  | Information about Holt Software's courses for teachers  | Information about how to contact Holt Software  | Information about how students can purchase Holt Software's books and software  | Information about how schools and bookstores can purchase Holt Software's books and software

How do I handle mouse and keyboard input at the same time?


It is quite common to want to have your program accept either keyboard or mouse input at the same time. The question is, how do you do it?

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
  

[ Turing Home ] * [ Top of Page ] * [ Feedback ]