Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!uunet!mcsun!ukc!sys-uea!jrk From: jrk@sys.uea.ac.uk (Richard Kennaway) Newsgroups: comp.sys.mac.programmer Subject: character codes vs. key codes Summary: When to use charcodes, when to use keycodes? Message-ID: <1074@sys.uea.ac.uk> Date: 15 May 91 17:39:52 GMT Sender: jrk@sys.uea.ac.uk Lines: 38 When should one use key codes, and when character codes, to determine which key was typed? IM 1 says that one should normally use character codes, not keycodes, but sometimes one has no alternative but to use keycodes. For example, on the Extended Keyboard, the function keys F1-F15 all generate the same character code, and can only be distinguished by their keycodes. In some cases, the answer is fairly clear. For determining whether a key event is a visible character, to be inserted into a piece of text, one should use the character code. The character codes for return and control-M are the same, as are those for enter and control-C, so those must be detected by key codes (unless you're writing a terminal emulator - outside that context, the relation between control-M and return is of only historical significance). Identifying F1-F15 requires the key code. Distinguishing keypad characters from main keyboard characters requires key codes. What should one do in the case of arrow keys? The escape key? (The latter is probably ambiguous with control-[, but I havent tested this.) The problem with using key codes is that the relation between key codes and character codes may vary from one keyboard to another. According to the latest version of TN 160, an extra level of code mapping has recently been introduced between the physical keystroke and the event record, adding a further source of uncertainty about what the key codes and character codes that my program sees really mean. I can't experiment with all the different keyboards and Systems that exist. Are there general rules which will work in all cases? I'm not wanting to do anything low-level or complicated, like using the keyboard to control a video game, just a simple text editor that correctly handles every possible key event on all Macs, all keyboards, and all Systems. Is this possible? -- Richard Kennaway SYS, University of East Anglia, Norwich, U.K. Internet: jrk@sys.uea.ac.uk uucp: ...mcsun!ukc!uea-sys!jrk