The Window Tool Kit

6 Keyboard Interrupt Characters

When you type a character at the keyboard, the character is normally queued on the mouse input stream that is associated with the current value of the function keyboard-input. If you define a keyboard interrupt character for a mouse input stream, the character is not queued on the stream if it is encountered. Instead, a user-defined function that you have associated with the keyboard interrupt character is invoked. You can use keyboard interrupt characters to provide a means of interrupting the current code action.

This chapter shows how to create a keyboard interrupt character function, or handler, for two interrupt characters,#\ETX (^C) and#\EOT (^D), and to rebind the value of mouse input stream associated withkeyboard-input. The keyboard interrupt character functions are invoked when you type the associated interrupt characters. Note that the functions associated with the interrupt character must take the arguments mouse-input-stream and character, even if these arguments are not used.

The global flag*exit-window-p* determines when you exit from the main functionshow-interrupt-char. You need to know when the main function exits so that you can rebind the stream associated withkeyboard-input to its default value. If you did not restore the value of this stream, you could not type to Lisp after the function exits.

(defvar *exit-window-p*)
;; Create a new window if you don't have one already.
(setq *window* (make-window :width 200 :height 50))

;; If a ^D is typed, print a message. (defun interrupt-handler-1 (mouse-input-stream character) (declare (ignore mouse-input-stream character)) (format *window* "^D"))

;; If a ^C is typed, print a message and set exit flag to t. (defun interrupt-handler-2 (mouse-input-stream character) (declare (ignore mouse-input-stream character)) (format *window* "^C") (setq *exit-window-p* t))

;; Associate functions with interrupt characters. (setf (mouse-input-stream-interrupt-char *window* #\EOT) #'interrupt-handler-1) (setf (mouse-input-stream-interrupt-char *window* #\ETX) #'interrupt-handler-2)

;; Redirect keyboard input, and wait until exit character ;; is typed. (defun show-interrupt-char (window) (let ((default-keyboard-input (keyboard-input)) event) (unwind-protect (progn (clear-bitmap window) (setf (stream-position window) (make-position 0 10)) (setq *exit-window-p* nil) ;; Bind keyboard-input to the window. (setf (keyboard-input) window) ;; Loop until flag is set by interrupt character. (loop until *exit-window-p*)) (setf (keyboard-input) default-keyboard-input))))

;; Invoke the function. (show-interrupt-char *window*)

Type the control characters^D and^C. These interrupt characters invoke interrupt handler functions, and you see text in the window. While control characters are often used as interrupt characters, you can use any type of characters for this purpose.

If you wish to disassociate a keyboard interrupt character from an associated stream and interrupt function, type the following expression:

(setf (mouse-input-stream-interrupt-char *window* #\EOT) nil)
If you now callshow-interrupt-char and type the keyboard interrupt character^D, you will not see text in the window.

For more information on using mouse input streams, see Chapter 3, "The Mouse".


The Window Tool Kit - 9 SEP 1996

Generated with Harlequin WebMaker