Date: 02-16-2022
Return to Index
created by gbSnippets
'Programmers often want to manage keystrokes, rather than accept the default
'actions of Windows - which is to handle all keyboard input, interpreting some
'keystrokes as input and some as Dialog navigation keys.
'This library shows 3 ways to capture a TAB key - allowing the programmer to
'take custom action, including preventing focus moving to another control.
'1. Using WM_NextDlgCtl (no subclassing)
'2. Using WM_KeyDown (within subclass procedure)
'3. Using WM_GetDlgCode (within subclass procedure)
'This snippets shows how to implement #1.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'Credit: Edwin Knoppert
'When a TAB key is pressed in a dialog, the WM_NextDlgCtl message is sent.
'A programmer can inercept that message, put in his own response to the TAB,
'and stop/allow the TAB to be processed normally (shifting focus to another control)
'This is done without subclassing. Simply insert this code into the callback function.
Select Case CB.Msg
Case %WM_NEXTDLGCTL
If GetFocus = hRichEdit Then
Function = 1
End If
'Compilable Example: (Jose Includes)
'This example show how to use the WM_NextDlgCtl message to control an applicaton's
'response to the user pressing the TAB key. Optional code is given, showing how to
'insert the TAB character into a RichEdit control when the TAB key is pressed. A listbox
'control is included in the example - showing that this method for TAB capture does not
'work for controls other than edit controls.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
%ID_RichEdit = 500 :%ID_ListBox = 501
Global hDlg As Dword, hRichEdit As Dword, OldProc&, hListBox as Dword
Function PBMain() As Long
Local style&, buf$
Dim s(2) as String
s(0)="One":s(1)="Two":s(3)="Three"
buf$ = "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Test Code",300,300,200,155, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Paste", 30,10,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add ListBox, hDlg, %ID_ListBox, s(), 20,40,80,100
Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,110,40,80,100, style&
SendMessage hRichEdit, %EM_SETEVENTMASK, 0, %ENM_SELCHANGE Or %ENM_CHANGE Or %ENM_LINK Or %ENM_KeyEvents
Control Handle hDlg, %ID_RichEdit To hRichEdit
Control Handle hDlg, %ID_ListBox To hListBox
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_NEXTDLGCTL
Select Case GetFocus
Case hRichEdit
Function = 1 'works - says no more processing required
Case hListBox
Function = 1 'doesn't work for listbox
End Select
End Select
End Function
'gbs_00402
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm