User Tools

Site Tools


Catching Every Key: -pause-, -keytype-, and -group-

Occasionally, it is useful to process individual keypresses without waiting for a NEXT key. We have already discussed such typical examples as moving a cursor and choosing a topic from an index. These examples used a “long 1” with an -arrow- in order to catch each keypress. There is another way to do this, involving the -pause- command which was introduced in Chapter 2 in connection with creating displays, particularly timed animations. As was pointed out in the discus- sion of the -jkey- command in the present chapter, the system variable “key” contains a number corresponding to the most recent key pressed by the student. For example, if the student presses the letter “d”, the system variable “key” will have the numerical value 4 (since d is the 4th letter in the alphabet). Putting these notions together, we have the following kind of structure:

write Press "d", please. pause writec key≠4,You didn't press d.,Good!

The blank -pause- statement (“blank” in the sense of having no tag) causes TUTOR to wait for the student to press a key. Any key will cause TUTOR to move past the -pause- to the next statement.

In the example shown, the -pause- is followed by a -writec- conditional on “key≠4”. This -writec- can be written in more readable form by replacing the “4” with a “d”:

write Press "d", please. pause writec key≠"d",You didn't press d.,Good!

Enclosing the d with (double) quote marks is taken in calculational expressions to mean the number 4. Similarly, (v3⇐z“) will assign the value 26 to v3. If the student presses 0 or 1, “key” will have the numerical value 27 or 28 respectively. That is, the 26 letters are followed by the numbers 0 through 9, then come various punctuation marks. If the student presses the plus key, “key” will have the numerical value ”+“, which happens to be 37.

If the student presses a capital D, “key” will have the value 64+“d”, or 68. The shifted or upper case letters have “key” value 64 greater than the corresponding lower-case letters. Caution: some common keys such as parentheses have key numbers smaller than 64 despite requiring the shift key to type them. The most commonly used characters (lower-case letters, numbers, and common punctuation marks) have key numbers less than 64, independent of whether they are typed using the shift key. As for the function keys (NEXT, BACK, HELP', etc.), we have seen (in connection with the -jkey- command) that the corresponding key numbers are given by next, back, helpl, etc., as in:

goto key= helpl,yes,no

No quote marks are used for the function keys.

A more convenient way to determine which key has been pressed is to use a -keytype- command. Consider a cursor-moving procedure:

define num=v5,x=v1,y-v2,dx=10,dy=10 unit cursor pause keytype num,d,e,w,q,a,z,x,c goto num,cursor,x calcs num-1,y⇐y,y+dy,y+dy,y+dy,y,y-dy,y-dy,y-dy

The -keytype- command searches through the listed keys (d, e, w, q, a, z, x, and c in this case) and, similar to the -match- command, sets “num” to -1 (if the key is not found in this list) or to 0, 1, 2, 3, etc. (if it is found). If the student presses d, “num” will be set to 0; if the student presses c, “num” will be 7; and if he or she presses D, “num” will be set to -1. The -goto- statement effectively causes all unlisted keys to be ignored.

Note that no quote marks are used in specifying keys in a -keytypecommand. Capital letters and function keys may also be listed:

keytype v3,a,A,b,B,next,data,timeup

While the -keytype- command is most often used in conjunction with a -pause- command, it can also be used in association with an -arrowcommand or any time that you want to find out which key was pressed most recently. The function key timeup is one generated by TUTOR when a timing key is “pressed” as the result of an earlier -time- command or timed -pause- command (see Chapter 2).

Just as the -list- command can be used to specify a set of synonomous words and numbers for use in -answer- and -match-, so there is a -groupcommand available for specifying synonomous keys for use in a -keytype- command:

define keynum=v23,algkey=v24 group algebra,x,y,z keytype keynum,a,b,algebra,help ⇩ ⇩ ⇩ ⇩ 0 1 2 3

If the student presses any of the keys x, y, or z, the variable “keynum” will be assigned the value 2. An additional -keytype- command can be used to separate members of a group:

keytype keynum,a,b,algebra,help goto keynum,none,ua,ub,alg,somehelp . . . unit alg keytype algkey,x,y,z . .

Some particularly useful -group- definitions are built-in. Without specifying these definitions with your own -group- commands, you can (in a -keytype- command) refer to these groups in the following ways:

alpha all 52 lower-case and upper-case letters numeric 0 through 9 funct function keys (next,help,etc.)

An example of the use of these built-in groups might be “keytype v45,funct,a,b,c”. You can also use previously defined or built-in groups to define new groups:

group mine,a,b,c,help group ours,mine,d,e,f group all,A,B,C,ours,numeric,funct
It is important to note that if you use a -pause-, the key pressed will not cause the associated character to appear on the student's screen. You are in complete control. You may write something on the screen or not, as you choose. Only if you use an -arrow- will the standard key display take place (with the associated ERASE and other standard typing features available). Similarly, if you press HELP, you will not automatically branch to a unit specified by a previous -help- command, because a blank. -pause- gives you every key, function key or not.

There is a variant of the -pause- command which is usually more useful than the blank -pause-. You can define which keys are to be accepted, and all other keys will be ignored:

. . . next umore help discuss data tables . . pause keys=d,D,next,term,help.help1 . . .

Any key not listed here is completely ignored, as though the student had not pressed it. Of the function keys listed, the HELP key will take the student to unit “discuss”, since you have already specified what you want the HELP key to do. Note that this is not possible with a blank -pausewhich catches all keys. Similarly, what the TERM key will do has been predefined (the student will be asked “what term?”). But the DATA key will be ignored since it is not listed in the -pause- statement, and the student cannot reach unit “tables” with the DATA key until he or she has passed the -pause-. Pressing d, D, NEXT, or HELP1 will take the student past the -pause-. The NEXT key is rather special here in that the preceeding specification “next umore”, unlike “help discuss”, tells TUTOR what to do when the present main unit has been completed. Thus, pressing NEXT here takes us past the -pause- instead of branching us immediately to a different unit as HELP does.

You may prefer not to ignore the HELP key nor to use it to access unit “discuss”. In this case, the statement “help discuss” must follow the -pause- statement, or a “help q” must precede the -pause- in order to quit specifying a help unit.

Touching the Screen

plato/tutor/catching_every_key.txt · Last modified: 2023/08/05 18:55 by Site Administrator