Microsoft KB Archive/69046

= Predefined Keys Trap All SHIFT Combinations; PEEK SHIFT Status =

Article ID: 69046

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q69046



SUMMARY
The predefined key traps KEY(0) through KEY(14), KEY(30), and KEY(31) are active no matter what combinations of the SHIFT, CTRL, ALT, CAPS LOCK, and NUM LOCK keys are active. If you want to distinguish and trap both the SHIFTed and unSHIFTed status for a given key, you must either set up two user-defined key traps, or check the contents of the SHIFT register while the predefined key is being trapped.



MORE INFORMATION
The following program demonstrates that even though a user-defined key trap, ON KEY(15), is activated for the SHIFT+DOWN ARROW key combination, the trap is never executed because the predefined DOWN ARROW key trap, ON KEY(14), takes precedence:

Code Example 1
KEY 15, CHR$(3) + &quot;P&quot; ON KEY(15) GOSUB ShiftArrow KEY(15) ON ON KEY(14) GOSUB DownArrow KEY(14) ON CLS DO a$ = INKEY$ LOOP UNTIL a$ = &quot;q&quot;

KEY(15) OFF KEY(14) OFF END ShiftArrow: PRINT &quot;Shift-Down arrow...&quot; RETURN DownArrow: PRINT &quot;Down arrow...&quot; RETURN The next program demonstrates how to use the PEEK function to determine if the SHIFT key is currently being pressed when the key trap occurs:

Code Example 2
ON KEY(14) GOSUB ArrowKey KEY(14) ON CLS DO a$ = INKEY$ LOOP UNTIL a$ = &quot;q&quot; KEY(14) OFF END ArrowKey: DEF SEG = 0 IF (PEEK(1047) AND 3) = 0 THEN PRINT &quot;Down arrow...&quot; ELSE PRINT &quot;Shift-Down arrow...&quot; END IF   DEF SEG RETURN The disadvantage of the method in example 2 is that if anything should happen to delay the time between the key press and the time that the trap routine is called (for example, if an INPUT statement is being executed when the key is pressed), then the user may release the SHIFT key before it can be detected.

The last option is to invoke a user-defined key trap for both the SHIFTed and unSHIFTed keys; and not to use the predefined key trap.

