! keyboard.inf - some general keyboard reading routines. ! ZBefunge - a Befunge interpreter for a Z Machine. ! Copyright 1998 Francis Irving. ! Changed a bit 2002, 2003 by Martin Bays for ZedFunge ! This source code is distributed under the GNU general public license, ! version 2 or later (at your option). See the file COPYING for ! full information. [ ReadNum c v; !***MDB*** while ((c=ReadKey()) ~= ASCII_CR or ASCII_LF) if (c>=48 && c<=57) { print (char) c; v=10*v + c-48; } return v; ]; ! Ask for yes/no and return true/false ! If default is 'y' or 'n', default to that response [ YesNo default code; @read_char 1 -> code; while ((~~default) && code ~= 'y' or 'Y' or 'n' or 'N') { @read_char 1 -> code; } if (code == 'y' or 'Y') rtrue; if (code == 'n' or 'N') rfalse; if (default == 'y' or 'Y') rtrue; rfalse; ]; ! Read a single char from the keyboard and return its code. [ ReadKey code; code = PreFunctionise(); if (code >= 0) return code; @read_char 1 -> code; code = Functionise(code); if (code == -2) return ReadKey(code); return code; ]; ! Used in ReadTimedKey below global timed_out; [ KeyInterrupt; timed_out = 1; rtrue; ]; ! Read a key, waiting for 1/10th sec. If timed out, return -1 [ ReadTimedKey code; code = PreFunctionise(); if (code >= 0) return code; timed_out = 0; @read_char 1 1 KeyInterrupt code; if (timed_out) code = -1; else { code = Functionise(code); if (code == -2) return ReadTimedKey(code); } return code; ]; global function_escape = false; global function_escape_mid_cancel = false; global f_pressed; global after_f_pressed; ! Allow user to type 'F' '9' for F9 etc. [ PreFunctionise; if (function_escape_mid_cancel) { function_escape_mid_cancel = false; return after_f_pressed; } return -1; ]; [ Functionise code; if (code < 0) return code; if (function_escape) { function_escape = false; switch (code) { '1' to '9': return (code - '1' + FUNCTION_1); '0': return FUNCTION_10; 'h', 'H': return FUNCTION_HIGH; 'l', 'L': return FUNCTION_LOW; 'y', 'Y': return FUNCTION_YANK; 'c', 'Y': return FUNCTION_COPY; 'd', 'D': return FUNCTION_KILL; 'x', 'X': return FUNCTION_CUT; 'p', 'P': return FUNCTION_PUT; 'v', 'V': return FUNCTION_PASTE; 'b', 'B': return FUNCTION_BREAK; 'o', 'O': return FUNCTION_ORIGIN; 'f', 'F': return FUNCTION_FILE_OVERLAY; } after_f_pressed = code; function_escape_mid_cancel = true; return f_pressed; } if (code == 'F') !***MDB*** no longer allow lower case 'f' - used in strings too often, would result in accidental commands (e.g. "fluffy" - go low, uf, yank) { f_pressed = code; function_escape = true; return -2; } return code; ];