Author Topic: [XDrX-Hook]Inject hooks into ACAD and monitor keyboard and mouse messages  (Read 807 times)

0 Members and 1 Guest are viewing this topic.

xdcad

  • Bull Frog
  • Posts: 424
Let Lisp to Control the WINDOWS message loop (inject hooks into ACAD) and monitor keyboard and mouse messages.


Regarding the message loop of the WINDOWS system, you can search online and refer to the literature.

All software in the WINDOWS system interacts with the system through the message loop. When we press the keyboard or move the mouse, the WINDOWS system will send the message to AUTOCAD, and AUTOCAD will accept the message processing and return the information to the system.

The system keyboard message received by AUTOCAD can be intercepted through the hook related functions of ARX. After the application process, the message can continue to be passed to AUTOCAD for subsequent operations, or it can be intercepted so that AUTOCAD does not know that this "message" has come, and AUTOCAD can also You cannot continue to respond to messages and operate.

The general hook function is defined as follows:

Code - C++: [Select]
  1. BOOL HOOK(MSG *pMsg)
  2. {
  3.         switch(pMsg->message){
  4.         case WM_KEYDOWN:
  5.         case WM_SYSKEYDOWN:
  6.                 if(...)
  7.                 {
  8.                         return TRUE; // TRUE intercepts the message and does not allow AUTOCAD to continue processing.
  9.                 }
  10.                 break;
  11.         }
  12.         return FALSE; // Return FALSE, AUTOCAD can continue to accept the message
  13. }


Can A/VLISP also intercept message processing? No, LISP does not provide such a function.

The above operations are completed through the custom functions provided by the XDRX API to LISP.

The "hook" related functions provided by the API are:

Code - Auto/Visual Lisp: [Select]
  1. xdrx-hook-register
  2. xdrx-hook-enable
  3. xdrx-hook-status
  4. xdrx-hook-remove
  5. xdrx-hook-tracemouse
  6. xdrx-hook-block-messageloop
  7. xdrx-system-keystate
  8. xdrx-system-ctrl-down
  9. xdrx-system-ctrl-shift-down
  10. xdrx-system-ctrl-alt-shift-down
  11. xd::hook:register
  12. xd::hook:enable
  13. xd::hook:remove
  14.  

The function of the xdrx-hook-block-messageloop function is equivalent to the return TRUE mentioned above, and the termination message is continued to AUTOCAD.

The typical hook callback function structure of LISP is as follows:

Code - Auto/Visual Lisp: [Select]
  1. (defun hook (hwnd msg wparam lparam time pos)
  2. ;|
  3. hwnd -- window handle
  4. msg --- message
  5. wparam -- main parameter
  6. lparam ---- second parameter
  7. time ------- time of occurrence
  8. pos ------- mouse screen coordinates
  9. |;
  10.   (cond ((= msg WM_KEYUP) ; key up
  11.         )
  12.         ((= msg WM_KEYDOWN) ;Key pressed
  13.          (setq txt (chr wparam))
  14.         )
  15.         ((= msg WM_SYSKEYDOWN)) ;System key pressed
  16.         ((= msg WM_CHAR)
  17.         )
  18.         ((= msg WM_MOUSEWHEEL) ; Turn the wheel
  19.         )
  20.         ((= msg WM_LBUTTONUP) ;The left mouse button pops up
  21.         )
  22.         ((= msg WM_LBUTTONDOWN) ;Left mouse button pressed
  23.         )
  24.         ((= msg WM_LBUTTONDBLCLK) ;Double left mouse button click
  25.         )
  26.         ((= msg WM_RBUTTONUP) ; Right mouse button pops up
  27.         )
  28.         ((= msg WM_RBUTTONDOWN) ;Right mouse button pressed
  29.         )
  30.         ((= msg WM_RBUTTONDBLCLK) ;right mouse button double click
  31.         )
  32.         ((= msg WM_MBUTTONUP) ;Middle mouse button pops up
  33.         )
  34.         ((= msg WM_MBUTTONDOWN) ;Middle mouse button pressed
  35.         )
  36.         ((= msg WM_MBUTTONDBLCLK) ;Middle mouse button double click
  37.         )
  38.         ((= msg WM_MOUSEMOVE) ;mouse movement
  39.         )
  40.         ((= msg WM_PAINT) ;Screen drawing
  41.         )
  42.         ((= msg WM_HOTKEY) ;Hotkey
  43.         )
  44.         ((= msg WM_INPUT) ;Input
  45.         )
  46.         ((= msg WM_VSCROLL) ;Screen scrolling
  47.         )
  48.   )
  49. )

The following is a program description of capturing mouse hotkeys through OpenDCL:
The following program will write the keyboard keys, including CTRL, SHIFT, ALT and letter key combinations, into the text box in real time.



Here is the implementation code:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.   (setq        odcl-data
  3.          '("YWt6A2YXAAAh3q+ZBuKTJsUwKi9qI/1OYh86Xl9frzx8ON3ysnVmUlK6Jtb29vdoaHSydoxmezrS"
  4. "zu26uLTmFh8Xn02Pa1XdcwgC/mE+nNVNeOsP4U0OYRVmUFYev6KaoEeVQohAGZHLqZss4OA1hb32"
  5. "XvRmZK96v4RQkzt03ZY5fKlu6ghxxE9+Yf6XHN46AIvKxNonZOif+jIW6Ndfursu3cocrGlo7T+X"
  6. "97byUBdLdXA3AXEk3qO42tk1KGTJFxNPUIOZ20EUKInmSx8nrRtSWBtSX9NoZxFIgOwcQxgusqYS"
  7. "5KHzYX/DJU/keNTfAla+glsQ9HC15/hPVu2ralRpOJjZ1u1W+p0sbZiamORv/ZTg5rEiQE8orfOD"
  8. "JkIec85jq9xjPRbrbXK3Jjw4JZswHzqklMm1nIjBy9a4IgjI07Lwy+U0hcJBiodBxNI8I1uCtOAR"
  9. "nIv8jjW8sXeBWgWd+oNASYAbmTllLEkuElspmdlIGcyh37bPbjTvm3HQ6y7Fk+PA6EXON2Alc9IZ"
  10. "MOvg7K7zNdZxnPRCObuXLnAAaz4nGRHJQyoVkglnU77Tp3ArOsVyxpwty+6b6hpH9rWcwDGipSeT"
  11. "7fKJ5dQtMfensXZB37ZBWIL2jj28W+rnX6kXfBOWunDHBXMledoDNuBp3lAklUwHN29gytyahdBN"
  12. "ojGdffpHsetqm6XKsVQFQtqPd2UxFALtsKc/jZkSgbigKaSxaVVw54vh9hmhko3nh+FTOBkOY9Oh"
  13. "F4XhrJ25F0E517zwbxBdAvpOApCGxpUcDUilqlD01t0MsLZjro9VTY5+KfDMVVlbwU8ab+AXucpS"
  14. "XbKXGQROA+YgMkHYGWmH5VoC5A78Tq2XUJ/NUCLfdQIyGgCmbJUw96bW744QN742SL/Cdpc7Tlga"
  15. "7AiW3xRzuvhuH7llCyh5wq40Jhii3/StZFHfJRhAlTD7E7GyIdBww8+DAK+F9O0hUWOnNZnW3Idw"
  16. "I8fRD0OrI+6OYMyB4IitP4gVNimIR6CabbaoY/nw6r3TXU0me2MX34PEC9oO/6o11/moId1gJXKK"
  17. "LPEWIEMM1DkSndf5AImaqx2Tq72C65ou8TUmUYv2UzFDfHgE1zla0WBHq13Ej6qdRIyUH6clPYtG"
  18. "eckEor8E1zkKB5QvFN7pBAYBk9FcSWEWshybuytoVdGRhauciBQBoQWrtDTBgccIJionCicagcuo"
  19. "JuonysMU7zviFU3Z1jkM98QpS4G5gQa6nyj1ySaaW89QlG+PKnEDg39nvMRW")
  20.   )
  21.   (defun c:hook-quit#OnClicked (/)
  22.     (xd::hook:remove "_hook-1")
  23.     (dcl-form-close hotkey-odcl/Form1)
  24.     (princ)
  25.   )
  26.   (defun c:hotkey-odcl/Form1#OnClose (UpperLeftX UpperLeftY /)
  27.     (xd::hook:remove "_hook-1")
  28.     (dcl-form-close hotkey-odcl/Form1)
  29.     (princ)
  30.   )
  31.   (defun c:hotkey-odcl/Form1#OnCancelClose (Reason /)
  32.     (xd::hook:remove "_hook-1")
  33.     (dcl-form-close hotkey-odcl/Form1)
  34.     (princ)
  35.   )
  36.   (defun c:hotkey-odcl/Form1#OnCancel (/)
  37.     (xd::hook:remove "_hook-1")
  38.     (dcl-form-close hotkey-odcl/Form1)
  39.     (princ)
  40.   )
  41.  
  42.   (defun c:var1#OnSetFocus (/)
  43.     (setq #var-name (dcl-Control-GetProperty var1 "VarName"))
  44.   )
  45.   (defun c:var2#OnSetFocus (/)
  46.     (setq #var-name (dcl-Control-GetProperty var2 "VarName"))
  47.   )
  48.   (defun c:var3#OnSetFocus (/)
  49.     (setq #var-name (dcl-Control-GetProperty var3 "VarName"))
  50.   )
  51.   (defun c:var4#OnSetFocus (/)
  52.     (setq #var-name (dcl-Control-GetProperty var4 "VarName"))
  53.   )
  54.   (defun _hook-1 (hwnd msg wparam lparam time pos)
  55.     (and (dcl-Form-IsActive hotkey-odcl/Form1)
  56.          (cond ((= msg WM_KEYDOWN)
  57.                 (if (and (> wparam 46)
  58.                          (< wparam 91)
  59.                          (/= "" #var-name)
  60.                     )
  61.                   (progn
  62.                     (setq txt (chr wparam))
  63.                     (cond
  64.                       ((xdrx-system-ctrl-down)
  65.                        (dcl-Control-SetText
  66.                          (eval (read #var-name))
  67.                          (strcat "CTRL+" txt)
  68.                        )
  69.                       )
  70.                       ((xdrx-system-shift-down)
  71.                        (dcl-Control-SetText
  72.                          (eval (read #var-name))
  73.                          (strcat "SHIFT+" txt)
  74.                        )
  75.                       )
  76.                       ((xdrx-system-ctrl-shift-down)
  77.                        (dcl-Control-SetText
  78.                          (eval (read #var-name))
  79.                          (strcat "CTRL+SHIFT+" txt)
  80.                        )
  81.                       )
  82.                       ((xdrx-system-ctrl-alt-shift-down)
  83.                        (dcl-Control-SetText
  84.                          (eval (read #var-name))
  85.                          (strcat "CTRL+ALT+SHIFT+" txt)
  86.                        )
  87.                       )
  88.                     )
  89.                     (xdrx-hook-block-messageloop t) ;;Block this message and let AUTOCAD continue processing.
  90.                   )
  91.                 )
  92.                )
  93.                (T)
  94.          )
  95.     )
  96.   )
  97.   (setq #var-name "")
  98.   (xd::hook:register "_hook-1" t "dd")
  99.   (dcl-project-import odcl-data "123456")
  100.   (dcl-Form-Show hotkey-odcl/Form1)
  101.   (princ)
  102. )

=============

The above LISP code uses the XDRX-API, which can be downloaded from https://github.com/xdcad/XDrx-API

The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.
Modify message
« Last Edit: November 18, 2023, 07:41:18 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
[XDrx-Sub Forum]
https://www.theswamp.org/index.php?board=78.0
https://github.com/xdcad/XDrx-API
http://bbs.xdcad.net