Hello:

I am having some troubles capturing key presses for menu shortcuts. To help illustrate, the below code is a simple dialog box without any controls or buttons. When I press a key on the keyboard, the message box fires. So far, so good.


.386
.model flat, stdcall  ;32 bit memory model
option casemap :none  ;case sensitive
include keytest.inc
.data
testit db "test",0
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
        invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_DIALOG1,NULL,addr DlgProc,NULL
invoke ExitProcess,0

;########################################################################

DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
mov eax,uMsg
.if eax==WM_INITDIALOG

.elseif eax==WM_COMMAND

.elseif eax==WM_KEYUP
invoke MessageBox,hWin,ADDR testit,ADDR testit,MB_OK

.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start


If I add an edit box to the form, then the edit box captures all of the key presses, and the message box does not pop up. If I also add a button to the form and change the focus from the edit box to the button with the tab key, the message box still will not pop up. I want to capture CTRL+G and CTRL+S etc to perform menu shortcuts. I have searched the forum and found many references to WM_KEYUP, WM_KEYDOWN, WM_CHAR etc. I can't seem to make any of them work. Whatever key I type either goes into one of my edit boxes when it has the focus or just makes the dialog box beep. How can I capture the CTRL+? keyboard sequence? A small code example would really be helpfull. Thanks in advance.

SS




Posted on 2006-11-22 21:29:29 by SideSwipe
The answer is to subclass the edit box (basically this involves writing a small window procedure for handling interesting messages, and telling the edit box to use that wndproc as the 'primary messagehandler')..
Search this board for more information, and if you're still lost, just set your shoelaces on fire and run in circles while screaming, or alternatively, ask again for more specific help :)
Posted on 2006-11-23 00:08:41 by Homer
Hello:

OK, I've subclassed the edit box to intercept keypresses. However, I have many edit boxes in my dialog box. Do I have to subclass each edit box and button? Can it not be done at the form level similar to the VB keypreview? It seems that each edit box can use the same NewWndProc that I created, but there are many invokes to GetDlgItem and SetWindowLong. When there are many controls on the form, would it be better to use a keyboard hook or some other method? Incidentally, I tried to subclass my whole dialog box, it didn't work.

Thanks,

SS
Posted on 2006-11-24 20:11:28 by SideSwipe
I'm not an expert in regards to Windows GUI code, however I can offer the following advice:
Having become aware that we can reroute windows messages for arbitrary window controls to a custom message handling proc, note that we can write such a proc to handle messages for N window controls - that is to say, we don't need lots of little wndprocs, we can write ONE wndproc which controls ALL window controls (since we can identify controls both by handle and by id) ... but you WILL have to point them all at that universal wndproc manually as long as we are using the SUBCLASSING method of window message hooking..
There's another options called SUPERCLASSING which I believe redirects messages for a CLASS of window controls, rather than individual windows, but don't quote me on that, check into it for yourself.. if I'm right, it implies that ALL newly-created windows of a given Class will automatically be pointed at your handler.
Posted on 2006-11-24 23:01:25 by Homer
You're right, Homer. Subclassing is for cuztomizing one window, while superclassing is for making a derivative of a given (existing) class. Every window of this new class will behave according to this class' settings. This way you can -for example- create 10 edit controls that accept only binary digits.
Posted on 2006-11-25 20:10:07 by ti_mo_n