Customer question: detectan F10 keypress

I received this question:


Is there any way to detect an F10 keypress in the KeyPress() event?

It looks like F10 is a system key of some sort (ALT?).



It’s similar to this post: How to hook command window keystrokes


I just modified the code a little to hook the WM_SYSKEYDOWN Notification  for a particular form


* 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

#define WM_SYSKEYDOWN                   0x0104

#define WM_SYSKEYUP                     0x0105

#define WM_SYSCHAR                      0x0106



PUBLIC oMyHook as MyHook







      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)


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


            BINDEVENT(thisform.HWnd, WM_SYSKEYDOWN,this,"HandleMsg")

            BINDEVENT(thisform.HWnd, WM_SYSKEYUP,this,"HandleMsg")

            BINDEVENT(thisform.HWnd, WM_SYSCHAR,this,"HandleMsg")

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

            LOCAL nRetvalue


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

            DO case

            CASE lParam=WM_KEYDOWN

            CASE lParam=WM_KEYUP

            CASE lParam=WM_CHAR



            RETURN nRetvalue




Comments (2)

Cancel reply

  1. John Henn says:

    We had that problem when we switched from DOS to windows. Since we never used F9 we simply did:

    on key label F10 Keyboard "{F9}" before the form runs. This redirects F10 which undoes the native menu command. Then we just put this code in the keypress method.

    if nKeyCode = -8 && F9 but now also F10

    <your Code>


    If you put this in the form keypress event be sure to set KeyPreview = .t.

  2. Interesting.  Here’s a good example of something I fight every day.  Calvin knows the innards of VFP and Windows quite well, so he goes straight for the more complex solution, but the simpler solution was offered by John.  At times I’ve found myself guilty of the same thing:  a customer asks a question and I give them a solution that’s too complex due to my knowledge of the system, but then they say "well, couldn’t I do so-and-so?" and I say "yes, that would work" while thinking to myself, "duh, why didn’t I think of the simple solution?"  And the answer is that it can be hard to divorce yourself of the technical knowledge that you have and the familiarity of working closer to the bare iron than a user does.  You tend to work a certain way and find certain solutions and they then to work a certain way and find certain solutions and sometimes their solutions are better than yours.  Here’s a situation where a user’s solution is sufficient and simpler.  Not a criticism of Calvin, just an acknowledgement of how developers see things through a certain prism and sometimes that can trip us up.

Skip to main content