[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
USER HOOKS
The GetKey function provides a feature which allows the programmer to
identify a procedure that will be continually called while the program
is idle, i.e. waiting for a keypress. For example, whenever the
program is waiting for keyboard input, a procedure could be called
that updates the time on the screen. This type of hook is referred to
as an Idle Hook.
Similarly, it is possible to identify a procedure that will be called
every time a key has been pressed. For example, the hooked procedure
could trap for the key F1 and call a help system. This avoids the need
to individually trap for the F1 key at every occurrence in the program
where GetKey is called. This type of hook is referred to as a Pressed
Hook.
Idle Hook
The first task is to build a procedure in your program that you want
to call every time the program is waiting for keyboard input. To use a
procedure as an Idle Hook it must:
1) Be a plain procedure with no passed parameters.
2) Be declared as a far procedure, i.e. precede the procedure with a
{$F+} compiler directive and succeed the procedure with the {$F-}
compiler directive.
Pressed Hook
As with the Idle Hook, the first task is to build the procedure that
is to be called every time a key is pressed. To use a procedure as a
Pressed Hook it must:
1) Be declared with one passed parameter. This must be a variable
parameter of type char, e.g.
PROCEDURE KEY_OVERRIDE(VAR CHH : CHAR);
2) Be declared as a far procedure, i.e. precede the procedure with a
{$F+} compiler directive and succeed the procedure with the {$F-}
compiler directive.
The hooked procedure is passed the key pressed by the user. The
procedure can update this character to effectively substitute the key
pressed by the user with a different one.
Turbo 5 Example
The example below illustrates the application of idle and pressed
hooks in a Turbo Pascal 5.0 program.
{$F+}
PROCEDURE SHOW_CLOCK;
BEGIN
WRITECENTER(1,WHITE,BLACK,TIME)
END;
PROCEDURE KEY_OVERRIDE(VAR CHK : CHAR);
BEGIN
CASE CHK OF
F1 : MAIN_HELP_PROCEDURE;
F5 : DISPLAY_SECURITY_SCREEN;
ALTX : EXIT_PROCEDURE;
END; {CASE}
END;
{$F-}
ASSIGN_IDLE_HOOK(SHOW_CLOCK);
ASSIGN_PRESSED_HOOK(KEY_OVERRIDE);
....
Turbo 4 Example
The example below illustrates the application of idle and pressed
hooks in a Turbo Pascal 4.0 program.
{$F+}
PROCEDURE SHOW_CLOCK;
BEGIN
WRITECENTER(1,WHITE,BLACK,TIME)
END;
PROCEDURE KEY_OVERRIDE(VAR CHK : CHAR);
BEGIN
CASE CHK OF
F1 : MAIN_HELP_PROCEDURE;
F5 : DISPLAY_SECURITY_SCREEN;
ALTX : EXIT_PROCEDURE;
END; {CASE}
END;
{$F-}
KTTT.IDLE_HOOK := @SHOW_CLOCK;
KTTT.PRESSED_HOOK := @KEY_OVERRIDE;
....
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson