How to hook command window keystrokes

A customer asked how to hook events for the command window.

The sample below uses BINDEVENT to hook the WM_KEYDOWN, WM_KEYUP and WM_CHAR messages. It changes all ‘a’ keystrokes to ‘b’.

* From C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\winuser.h

#define GWL_WNDPROC (-4)

#define WM_KEYDOWN 0x0100

#define WM_KEYUP 0x0101

#define WM_CHAR 0x0102

PUBLIC oMyHook as MyHook

oMyHook=NEWOBJECT("MyHook")

DEFINE CLASS MyHook AS session

      dwOrigWindProc=0

      dwShNotify=0

      PROCEDURE init

            DECLARE integer GetWindowLong IN WIN32API ;

                  integer hWnd, ;

                  integer nIndex

            DECLARE integer CallWindowProc IN WIN32API ;

                  integer lpPrevWndFunc, ;

                  integer hWnd,integer Msg,;

                  integer wParam,;

                  integer lParam

            THIS.dwOrigWindProc =GetWindowLong(_VFP.HWnd,GWL_WNDPROC)

            cWind="Command"

            WDOCKABLE(cWind,.f.) && make Command window not dockable

            DECLARE integer FindWindowEx IN WIN32API integer, integer,STRING,string

            hwndCmd=FindWindowEx(_screen.HWnd,0,0,cWind)

            BINDEVENT(hwndCmd, WM_KEYDOWN,this,"HandleMsg")

            BINDEVENT(hwndCmd, WM_KEYUP,this,"HandleMsg")

            BINDEVENT(hwndCmd, WM_CHAR,this,"HandleMsg")

      PROCEDURE HandleMsg(hWnd as Integer, msg as Integer, wParam as Integer, lParam as Integer)

            LOCAL nRetvalue

            nRetvalue=0

* ?PROGRAM(),msg,TRANSFORM(wParam,"@0x"),TRANSFORM(lParam,"@0x")," "

            IF wParam=ASC("a")

                  wParam=ASC("b")

            ENDIF

            DO case

            CASE lParam=WM_KEYDOWN

            CASE lParam=WM_KEYUP

            CASE lParam=WM_CHAR

            ENDCASE

            nRetvalue=CallWindowProc(this.dwOrigWindProc,hWnd,msg,wParam,lParam)

            RETURN nRetvalue

ENDDEFINE

Comments

  • Anonymous
    October 20, 2005
    The comment has been removed
  • Anonymous
    March 08, 2006
    A customer had a question and sent some code:
     
    PUBLIC oHandler
    oHandler = NEWOBJECT("StatusBarText")...