home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hookkbsm.zip / HOOKKBSM.DOC < prev    next >
Text File  |  1993-07-28  |  19KB  |  371 lines

  1. Document File for HOOKKBSM PROGRAM
  2.  
  3. From : Morton F. Kaplon  CIS : 73457,437
  4.        1047 Johnston Drive
  5.        Bethlehem PA 18017
  6.        (215)758-9686
  7.  
  8. Revised and Updated : Thu  07-22-1993
  9.  
  10. Subject : Hooking the Keyboard in OS/2 2.0, Assigning HotKeys,
  11.           MACROS:Recording and Playing Back KeyStrokes and Mouse Actions
  12.  
  13. INTRODUCTION and OVERVIEW
  14.  
  15.   HOOKKBSM is an OS/2 equivalent of a DOS TRS program. Operationally it
  16.   consists of two cooperating entities. One is the DLL(dynamic link library)
  17.   named HOOKDLSM.DLL and the other the executable HOOKKBSM.EXE. When HOOKKBSM
  18.   is loaded, it in turns loads HOOKDLSM into memory where it remains until it
  19.   is removed from memory by an explicit call (made by HOOKKBSM when it is
  20.   closed). Since HOOKDLSM is a dynamic link library, it can in principle
  21.   interact with programs other than the one which loaded it. In this
  22.   application, the DLL contains two functions. One named INITIALIZE which is a
  23.   specific link between it and HOOKKBSM, and the other named InputHook which
  24.   is called by the system whenver the WinGetMsg or the WinPeekMsg functions
  25.   are about to return a message. Thus the DLL has access to all messages in
  26.   the system message queue.
  27.  
  28.   When HOOKKBSM is loaded, it displays a small window named "Hook" in the
  29.   lower right Corner of the Desktop. In addition to providing evidence of the
  30.   Active existence of HOOKKBSM, the window is used to display messages when
  31.   operating in MacroRecord or MacroPlayBack mode and when the file containing
  32.   user assignment data has been changed and re-read into memory.
  33.  
  34.   In its role as a hot-key actuator it tests ALL WM_CHAR messages in the
  35.   message queue, the equivalent in DOS of hooking the Keyboard Interrupt. When
  36.   a key stroke meeting a HotKey definition is detected (one of the sequence
  37.   Shift-Alt/Ctrl-X), HOOKDLSM notifies HOOKKBSM by POSTing a message to it
  38.   with the mp1 parameter holding a flag indicating whether the Alt or Ctrl key
  39.   was struck along with the Shift Key and the parameter mp2 holding the Scan
  40.   Code of the Key X that was struck. HOOKKBSM takes action if the key X is
  41.   defined in its Data structures.
  42.  
  43.   Its role as a Macro Recorder is initiated by the user action of
  44.   pressing the Shift-Ctrl-LeftArrow combination. HOOKKBSM is notified of
  45.   this by the action above, and in turn Calls HOOKDLSM setting a flag to
  46.   ON that enables the recording of all messages from the system message
  47.   queue but WM_TIMER (its behavior seems unaffected if WM_TIMER is not
  48.   excluded). The messages are saved in a DataBuffer. Macro recording is
  49.   turned OFF by pressing the combination Shift-Ctrl-RightArrow, which
  50.   results in HOOKKBSM Calling the DLL and setting the flag to OFF. The
  51.   set of messages in the Data Buffer is played back by pressing the
  52.   Shift-Ctrl-Ins key. If the combination Shft-Ctl-PgDn is struck before
  53.   a new macro recording is started, the content of the buffer is saved,
  54.   in ASCII text form to a file named HOOKKBSM.MAC. The file is a set of
  55.   records, one for each message, in the format indicated below. This is
  56.   sort of like a "spy" program and allows one to see what goes on behind
  57.   the scenes, so to speak.
  58.  
  59. WinHandle MessageID msgParam1 msgParam2  msgTime  X_ScreenP Y_ScreenP TaskListName
  60.  
  61.   The set of Hot-Keys for which the User may define actions is that for which
  62.   X = 0,1,..,9,A,B,...,Y,Z (independent of case), a total of 72. Values of X =
  63.   1..9 on the White Keys on the KeyPad are reserved for a variety of program
  64.   defined uses as well as the key on the regular keyboard, having the symbol
  65.   "?" in the UpperCase position.
  66.  
  67.   The reason for requiring the triplet combination of Shift-Alt-X or
  68.   Shift-Ctrl-X is that besides the large number of pre-assigned keys in OS/2,
  69.   many programs use keystroke combinations for accessing specific features and
  70.   if the Hot Key is to be useful, it should not conflict. I can only remember
  71.   one program that I have seen over the years which used a Shift-Alt-X or
  72.   Shift-Ctrl-X combination and so it seems to be a fairly safe one, and one
  73.   that should not lead to conflicts. In filtering KeyStrokes the program
  74.   rejects all but the combinations Shift-Alt-X and Shift-Ctrl-X so that the
  75.   usual run of keystroke assignments of Alt-X or Ctrl-X and all Function key
  76.   combinations will be unaffected. Keystrokes are recognized on the down
  77.   stroke. I would note that the Shift-Alt or Shift-Ctrl combination is
  78.   particularly easy to access on those keyboards where the Shift Key sits
  79.   vertically between the Ctrl and Alt keys.
  80.  
  81.   For the User defined HotKeys, HOOKKBSM reads the User created file
  82.   HOOKKBSM.DAT which defines the actions associated with each Hot-Key. The
  83.   action taken is that of executing an assigned command which may be a COM,
  84.   EXE, CMD, or BAT file or loading an instance of the command processor for
  85.   either OS/2 or DOS. Thus you may activate a program or load an instance of
  86.   the command processor at the flick of a HotKey without having to access an
  87.   ICON. Included in the files is a sample HOOKKBSM.DAT(one that I use) which
  88.   defines the format to be followed and which the user should modify to
  89.   reflect his/her requirements.
  90.  
  91.   IF you edit the file HOOKKBSM.DAT when HOOKKBSM is active, you need only
  92.   use the key combination Shift-Alt-UpArrow(White) to activate the changes.
  93.   The "Hook" window in the lower right screen flashes "USER Updated" when this
  94.   is done. This update fixes an assumption about editors made previously.
  95.   Namely that files were not terminated with a Ctrl-Z. That was causing
  96.   errors when many editors (those coming with OS/2) were used that place
  97.   a Ctrl-Z at the EOF.
  98.  
  99.   In addition there are several features also assigned to Hot Keys which are
  100.   of a diagnostic or informational nature and may be of some educational use
  101.   in understanding some aspects of OS/2. Finally there are some housekeeping
  102.   aspects (removing the "TSR", and closing down the system) and one OS/2 PopUp
  103.   Command window included in the pre-assigned category.
  104.  
  105.   <--------------------- Pre-Assigned KEY ASSIGNMENTS ----------------------->
  106.   Shft-Alt-Del(wkp)      : unloads program (like unloading a TSR in DOS)
  107.   Shft-Alt-End(wkp)      : performs an orderly shut down of the system
  108.                            (same as from the DeskTop ShutDown Option)
  109.   Shft-Alt-UpArrow(wkp)  : Update DataStructures if HOOKKBSM.DAT changed
  110.   Shft-Alt-Ins(wkp)      : pops up a small OS/2 window in center of screen
  111.   Shft-Ctl-LftArrow(wkp) : Start Macro Recording
  112.   Shft-Ctl-RgtArrow(wkp) : Stop Macro Recording
  113.   Shft-Ctl-PgDn(wkp)     ; Write Last Macro as ASCII text to file HOOKKBSM.MAC
  114.   Shft-Ctl-Ins(wkp)      : Playback Macro Using Current Window SetUp
  115.   Shft-Ctl-?(tkb)        : Display Listing of User Defined HotKeys
  116.   Shft-Alt-?(tkb)        : Display Listing of Pre-assigned Hot Keys
  117.  
  118.   Shft-Alt-5(wkp)        : For window under Mouse,Displays Handle,WindowClass
  119.                          : Window Type(10h/20h bit),Parent and Owner
  120.   Shft-Ctl-5(wkp)        : WindowEnumerator from Window Under Mouse
  121.  
  122.   wkp = White Key Pad   tkb = typewriter key board
  123.   All hot-keys callable from anywhere, except Full Screen OS/2 or DOS sessions.
  124.  
  125.  
  126.   INSTALLATION
  127.  
  128.   1: Copy the file HOOKKBSM.EXE to a directory that is defined in your path
  129.      command.
  130.  
  131.   2: Copy the file HOOKDLSM.DLL to the directory C:\OS2\DLL
  132.  
  133.   3: a. Copy the file HOOKKBSM.DAT to the directory C:\OS2
  134.      b. Edit the file HOOKKBSM.DAT to fit your requirements.
  135.  
  136.         Instructions for format are included in the comments in this file.
  137.         This one as defined, uses the 4OS2 command processor. To use the
  138.         OS/2 command processor instead replace
  139.  
  140.          C:\4OS2\4OS2.EXE  WITH  C:\OS2\CMD.EXE
  141.  
  142.   4: a. Copy the file HOT-KEYS.PRG to C:\OS2
  143.      b. Copy the file HOT-KEYS.USR to C:\OS2
  144.      c. Edit the file HOT-KEYS.USR to reflect your assignments as made
  145.         in HOOKKBSM.DAT
  146.  
  147.   5: There are a variety of flags that affect the behavior of hookkbsm
  148.      that can be passed on the command line when it is started.
  149.      These can be passed in either upper or lower case and are:
  150.  
  151.      /scommandprocessorfilespec  commandprocessorfilespec is the
  152.        filespecification of the command processor to use when using
  153.        the Shift-Alt-Ins(whiteKeys) option to open an OS/2 window.
  154.        The default is the command processor defined by the COMSPEC
  155.        variable in config.sys. This is determined by the program
  156.        when loaded.
  157.  
  158.      /i This makes the window invisible. When any commands are to be
  159.         displayed in the window, it is made visible for those displays
  160.         and then restored to invisible
  161.  
  162.      /j The default behavior is for hookkbsm to be not Jumpable, that is
  163.         using Alt-Esc or Alt-Tab does not activate the hookkbsm window.
  164.         This switch makes it responsive to Alt-Esc or Alt-Tab
  165.  
  166.      /l The default. This positions the hookkbsm window in the lower left
  167.         corner with its defined size.
  168.  
  169.      /r This positions the hookkbsm window in the lower right corner
  170.         with its  defined size.
  171.  
  172.        If you want to use the OS/2 command processor
  173.  
  174.        START HOOKKBSM     starts with the defaults
  175.  
  176.          OR   If you use the 4OS2 command processor and it is stored in
  177.          c:\4os2\4os2.exe
  178.  
  179.        START HOOKKBSM /sC:\4OS2\4OS2.EXE
  180.  
  181.          OR   If you use yet a different command processor use its file
  182.               specification as the parameter on the command line instead
  183.               of C:\4OS2\4OS2.EXE.
  184.  
  185.        Example:
  186.        START HOOKKBSM /r /i   starts the window as invisible and in the
  187.                               lower right hand corner
  188.  
  189.   6:  OPTIONALLY you may place the starting command in a STARTUP.CMD file to
  190.        have the HOOK loaded automatically.
  191.  
  192. OVERVIEW
  193.  
  194.   FILES REQUIRED TO CREATE HOOKKBSM.EXE AND HOOKDLSM.DLL
  195.  
  196.   DOSWIN32.MAC  Macros and Equates used by Programs
  197.  
  198.   HOOKKBSM.ASM  Source for Executable, Assembled and Linked by MLC-W386.CMD
  199.  
  200.   HOOKDLSM.DEF  Define file needed by IMPLIB and linker for HOOKDLSM.ASM
  201.  
  202.   HOOKDLSM.ASM  Source for HOOKDLSM.DLL, Assembled and linked by DLL-W386.CMD
  203.  
  204.   DLL-W386.CMD  IMPLIB and HOOKDLSM.DEF create C:\TOOLKT20\OS2LIB\HOOKDLSM.LIB
  205.                 MASM then assembles HOOKDLSM.ASM, LINK386 produces HOOKDLSM.DLL
  206.                 and HOOKDLSM.DLL is copied to C:\OS2\DLL directory.
  207.  
  208.   MLC-W386.CMD  MASM assembles HOOKKBSM.ASM and LINK386 produces HOOKKBSM.EXE
  209.  
  210.   To ASSEMBLE and LINK use the directory holding the above files as the
  211.   default and the two commands below (in order given) to create the necessary
  212.   DLL and EXE files.
  213.  
  214.   DLL-W386  HOOKDLSM
  215.   MLC-W386  HOOKKBSM
  216.  
  217.   FILES REQUIRED TO EXECUTE HOOKKBSM.EXE
  218.  
  219.   HOT-KEYS.PRG  This is a text file called by the HotKey combination
  220.                 Shft-Alt-?..It displays a listing of the HARD CODED
  221.                 Key Assignments in a format contained in a Standard
  222.                 OS/2 window. It should not be edited and should be
  223.                 used as is. MUST BE LOCATED IN C:\OS2.
  224.  
  225.   HOT_KEYS.USR  This is a text file called by the HotKey combination
  226.                 Shft-Ctl-?. It must be created by the USER to reflect
  227.                 his/her key assignments as contained in HOOKKBSM.DAT.
  228.                 The file presented is a Template that contains space
  229.                 for all the User assignable HotKeys and which will
  230.                 be contained in a standard OS/2 text window.
  231.                 MUST BE LOCATED IN C:\OS2.
  232.  
  233.   HOOKKBSM.DAT  Text File assigning programs to key strokes-read by HOOKKBSM.
  234.                 The USER creates this file according to the structure outlined
  235.                 in the sample. MUST BE LOCATED IN C:\OS2. Edit the sample to
  236.                 fit your needs.
  237.  
  238.   HOOKDLSM.DLL  Dynamic Load File Created Above - must be in C:\OS2\DLL
  239.  
  240.   HOOKKBSM.EXE  Exec file created above
  241.  
  242.   DO NOT RENAME HOOKDLSM.* or HOOKKBSM.DAT as those names are coded into
  243.                 HOOKKBSM.ASM .
  244.  
  245.   The program is written in 32 bit Assembler. The assembler used is MASM 6.00B
  246.   including its built in MACROS for control structures and segment
  247.   definitions.
  248.  
  249.   The 32 bit linker (LINK386) and the 32 bit library, along with the necessary
  250.   INC files require the user to have the OS/2 TOOLKIT as well as MASM 6.00B to
  251.   assemble the program.
  252.  
  253.   PROGRAM METHOD
  254.  
  255.   The system message queue is hooked using the HK_INPUT parameter. The
  256.   function which samples the message queue is in HOOKDLSM.DLL. In the section
  257.   "Input Hook", p. 30-2 in the Programming Guide, Vol. II, it states : "The
  258.   system calls an input_hook function whenever the WinGetMsg or WinPeekMsg
  259.   functions is about to return a message."
  260.  
  261.   There are two procedures in HOOKDLSM.DLL. One is named Initialize and is
  262.   used to pass parameters between HOOKKBSM and HOOKDLSM. The other named
  263.   InputHook tests for WM_CHAR and when detected it further tests to see if the
  264.   key combination Shift-Alt-X or Shift-Ctrl-X was struck, with X (as defined
  265.   above) on the down stroke. If that criteria is met its POSTS to HOOKKBSM,
  266.   via the API function WinPostMsg, the message WM_USER+300h. The mp1 parameter
  267.   of that message holds a flag indicating whether Alt or Ctl was down and the
  268.   mp2 parameter has the scan code for the key X that was struck.
  269.  
  270.   IF at any time the user starts to record a Macro by hitting the
  271.   HotKeyCombination Shft-Ctrl-LeftArrow, then the program HOOKKBSM calls
  272.   the program HOOKDLSM and sets a flag in HOOKDLSM so that the procedure
  273.   InputHook also saves ALL messages in the system queue (but WM_TIMER)
  274.   to a memory Buffer. Omitting the WM_TIMER messages (its inclusion
  275.   seems to make no difference) saves buffer space. Hitting the
  276.   combination Shft-Ctrl-RightArrow results in HOOKKBSM resetting the
  277.   flag in HOOKDLSM that so the recording of all messages is turned OFF.
  278.   If the combination Shft-Ctl-PgDn is struck before a new macro
  279.   recording is started, the content of the buffer is saved, in ASCII
  280.   text form to a file named HOOKKBSM.MAC. The file is a set of records,
  281.   one for each message, in the format indicated below.
  282.  
  283. WinHandle MessageID msgParam1 msgParam2  msgTime  X_ScreenP Y_ScreenP TaskListName
  284.  
  285.   The main program, HOOKKBSM.EXE, a PM program, sets up the hook, receives the
  286.   message indicated above and takes the appropriate action depending on the
  287.   Key Stroke. The program is established as visible and is listed in the
  288.   Window List. The small window displayed is located in the lower right hand
  289.   corner of the Desktop. The program should be launched from an OS/2 window
  290.   with the START command, i.e. "START HOOKKBSM" or "START HOOKKBSM
  291.   C:\4OS2\4OS2.EXE" where the latter option uses the command processor
  292.   4os2.exe as the one to be loaded with the Shift-Alt-Ins key strokes. (Any
  293.   other valid ODS/2 2.0 command processor may loaded here in place of the
  294.   default c:\os2\cmd.exe). That command can be placed in a STARTUP.CMD file
  295.   for automatic loading.
  296.  
  297.   HOOKKBSM determines if the Key actually struck was assigned in the file
  298.   HOOKKBSM.DAT and if so reads its parameters into the appropriate data
  299.   structure required to execute the program.
  300.  
  301.   The key stroke combination Shift-Alt-End(white) closes down the system in
  302.   the same manner as choosing the ShutDown option from the DeskTop menu does.
  303.   Again you are relieved of hunting for space to access the desktop.
  304.  
  305.   The program automatically sets DPMI_DOS_API=ENABLED for programs whose
  306.   session type (DOS full or windowed) is defined as 4 or 7 in HOOKKBSM.dat.
  307.   This enables the Dos Protected Mode Interface for the DOS session. If the
  308.   Session type value of 0 is used, DPMI is not enabled as above. It is
  309.   interesting to note that setting that parameter is done by passing the
  310.   DosStartSession data structure an address holding the text string above,
  311.   even though the manual states that the field for a DOS session is reserved
  312.   and must be ZERO. Unfortunately, there are quite a few errors in the OS/2
  313.   Technical Library, but fortunately a lot of people (principally on
  314.   CompuServe) to inform you about them.
  315.  
  316.   The program also contains a NASCENT  Macro Record and Playback facility
  317.   which is rather limited in its capabilities. AT the present time the
  318.   PlayBack is limited to just the most recent Macro recorded. When Macros are
  319.   recorded, what is saved are the parameters of the message queue as contained
  320.   in the structure QMSG
  321.      QMSG    STRUC
  322.      qmsg_hwnd       DD      ?
  323.      qmsg_msg        DD      ?
  324.      qmsg_mp1        DD      ?
  325.      qmsg_mp2        DD      ?
  326.      qmsg_time       DD      ?
  327.      qmsg_ptl        DB      SIZE POINTL DUP (?)
  328.      qmsg_reserved   DD      ?
  329.      QMSG    ENDS
  330.  
  331.      POINTL  STRUC
  332.      ptl_x   DD      ?
  333.      ptl_y   DD      ?
  334.      POINTL  ENDS
  335.  
  336.   The first six components of QMSG make up each Macro RECORD. The first
  337.   element qmsg_hwnd is the handle of the window that was created during the
  338.   actual macro recording. IF a series of windows were opened during that
  339.   recording and then closed, when the macro is played back, the actual handle
  340.   assigned when windows are opened is not necessarily the same as that
  341.   contained in the message and thus the value in the macro record is of no use
  342.   in the playback. IF the window display is different during playback than
  343.   when recorded, mouse actions will be played back differently. Thus, in
  344.   playing back any macro one must be certain that the window set up initially
  345.   is the same as when recorded.
  346.  
  347.   The playback currently does not handle any other mouse actions than its
  348.   movement and the left mouse button actions.
  349.  
  350.   It also does not playback the Alt-Esc or Alt-Tab sequences, and only
  351.   plays back those involving CTrl-Esc if the undocumented message 82h is
  352.   trapped for playback. It also does incomplete PlayBacks in those cases
  353.   where two windows on screen are highlighted at the same time, such as
  354.   a Table of Contents window included when On Line documentation as
  355.   brought up by, for example by the command VIEW REXX from the OS/2
  356.   prompt. In these cases, and this is one of the problems puzzling me,
  357.   the keystrokes selecting commands from the Table of Contents appear to
  358.   be played back before the window is completely painted on the screen.
  359.   This appears more dramatically if you use Mouse movements.
  360.  
  361.   The problem does not occur, when the screen selected, has a single
  362.   highlighted component, such as HELP.
  363.  
  364.   The macros that record removing items, such as F3 from Table of Contents
  365.   Screens and ALt-F4 to close Containers show problems. Macros recognize
  366.   F3 in closing Table of Contents and also Esc, but do not play back
  367.   the Alt-F4 or Alt-Close(via the mouse) choices.
  368.  
  369.  
  370.  
  371.