1. Re: Newbie with a Ctrl-C problem
Robert Pilkington suggested:
> Or David, you could jump in and get the needed code
> [from EE] and give it to John sooner.
Here's the code. I've include the key codes as well. If you set the constant
TYPE_AHEAD to zero, it will kill the type-ahead. The KEY_CODE and SCAN_CODE
variables contain the actual key code/scan codes, in case you would rather
use them instead of the ASCII values.
A bit of explanation of the code. If you use DOS to read the key buffer AND
it contains a CTRL-C, DOS will automatically execute the CTRL-C interrupt.
This is what we want to avoid. Instead, my code checks to see if the next
keystroke is a CTRL-C. If it is, I zap the buffer, and manually return the
CTRL-C key code. Otherwise, I read the buffer normally. This way, DOS never
unloads a CTRL-C from the buffer, and the interrupt never gets triggered.
To "clear the buffer", I set the FIRST_INDEX and the LAST_INDEX to the same
values. Since they are pointers to a circular buffer, when the point to the
same location, the buffer is empty. Brutal, but efficient.
There are other routines in KEYS.E that people might find interesting. For
example, there are functions to return the state of the Alt/Ctrl/Shift keys.
Hope this helps!
-- David Cuny
-- modification from KEYS.E
global integer
KEY_CODE, -- key read from keyboard
SCAN_CODE -- scan code of key read
constant
TYPE_AHEAD = 1, -- if 0, no type ahead
KEY_BUFFER = 1054, -- keyboard buffer; circular queue
FIRST_INDEX = 1050, -- index to first key in buffer
LAST_INDEX = 1052 -- index to last key in buffer
-- key definitions
global constant
BACKSPACE = 8, TAB = 9, ENTER = 13, ESC = 27,
SHIFT_TAB = 271,
ALT_A = 286, ALT_B = 304, ALT_C = 302,
ALT_D = 288, ALT_E = 274, ALT_F = 289,
ALT_G = 290, ALT_H = 291, ALT_I = 279,
ALT_J = 292, ALT_K = 293, ALT_L = 294,
ALT_M = 306, ALT_N = 305, ALT_O = 280,
ALT_P = 281, ALT_Q = 272, ALT_R = 275,
ALT_S = 287, ALT_T = 276, ALT_U = 278,
ALT_V = 303, ALT_W = 273, ALT_X = 301,
ALT_Y = 277, ALT_Z = 300, ALT_1 = 376,
ALT_2 = 377, ALT_3 = 378, ALT_4 = 379,
ALT_5 = 380, ALT_6 = 381, ALT_7 = 382,
ALT_8 = 383, ALT_9 = 384, ALT_0 = 385,
CTRL_A = 01, CTRL_B = 02, CTRL_C = 03,
CTRL_D = 04, CTRL_E = 05, CTRL_F = 06,
CTRL_G = 07, CTRL_H = 08, CTRL_I = 09,
CTRL_J = 10, CTRL_K = 11, CTRL_L = 12,
CTRL_M = 13, CTRL_N = 14, CTRL_O = 15,
CTRL_P = 16, CTRL_Q = 17, CTRL_R = 18,
CTRL_S = 19, CTRL_T = 20, CTRL_U = 21,
CTRL_V = 22, CTRL_W = 23, CTRL_X = 24,
CTRL_Y = 25, CTRL_Z = 26,
F1 = 315, F2 = 316, F3 = 317, F4 = 318, F5 = 319,
F6 = 320, F7 = 321, F8 = 322, F9 = 323, F10 = 324,
F11 = 389, F12 = 390,
HOME = 327, UP = 328, PAGE_UP = 329, LEFT = 331,
RIGHT = 333, END = 335, DOWN = 336,
PAGE_DOWN = 337, INSERT = 338, DELETE = 339,
CTRL_LEFT = 371, CTRL_RIGHT = 372, CTRL_END = 373,
CTRL_HOME = 375, CTRL_PAGE_UP = 388, CTRL_PAGE_DOWN = 374,
CTRL_UP = 397, CTRL_DOWN = 401, CTRL_INSERT = 402,
CTRL_TAB = 404, CTRL_ENTER = 10,
ALT_TAB = 421,
ALT_PRESSED = 600, ALT_RELEASED = 601,
SHIFT_PRESSED = 603, SHIFT_RELEASED = 604
----------------------------------------------------------------------------
global function get_safe_key()
-- returns a key from the buffer, or 0 if there is no
-- key pending. handles CTRL_C without choking.
integer key_code
-- buffer is empty if indexes point to each other
if peek( FIRST_INDEX ) = peek( LAST_INDEX ) then
-- nothing in the buffer
return 0
end if
-- get the scan key and key code
SCAN_CODE = peek( KEY_BUFFER + peek( FIRST_INDEX ) - 29 )
KEY_CODE = peek( KEY_BUFFER + peek( FIRST_INDEX ) - 30 )
-- look for control-c
if SCAN_CODE = 46 and KEY_CODE = 3 then
-- clear the buffer (point the beginning to the end)
poke( FIRST_INDEX, peek( LAST_INDEX ) )
-- return a control C
return CTRL_C
else
-- read through the normal process
if TYPE_AHEAD then
return get_key()
else
-- get the key
key_code = get_key()
-- clear the buffer
poke( FIRST_INDEX, peek( LAST_INDEX ) )
-- return the key
return key_code
end if
end if
end function