Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "imm.inc"
Global hDlg as DWord
Global ghKeyb, hDlg AS DWord ' global handles
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call MainDlgProc
End Function
CallBack Function MainDlgProc()
Select Case CB.Msg
Case %WM_INITDIALOG
ghKeyb = SetWindowsHookEx(%WH_KEYBOARD, CODEPTR(KeyBoardProc), 0, GetCurrentThreadId)
Case %WM_DESTROY
UnhookWindowsHookEx ghKeyb
Case %WM_COMMAND
SELECT Case CB.CtlMsg
Case %BN_CLICKED
IF CBCTL = %IDOK Then
DIALOG End CB.Hndl
End IF
End SELECT
Case %WM_HELP
CONTROL SET TEXT CBHNDL, 1001, "F1 (help)"
Case %WM_USER + 101
Local sTmp As String
' check shift mode info by processing lParam
IF (CB.lParam AND %MOD_ALT) Then sTmp = "Alt+"
IF (CB.lParam AND %MOD_CONTROL) Then sTmp = sTmp & "Ctrl+"
IF (CB.lParam AND %MOD_SHIFT) Then sTmp = sTmp & "Shift+"
' now check which key (virt. key code or ASCII code) has been pressed
SELECT Case As Long CBWPARAM
Case 8 : sTmp = sTmp & "BackSpace"
Case 9 : sTmp = sTmp & "Tab"
Case %VK_PGUP : sTmp = sTmp & "PgUp"
Case %VK_PGDN : sTmp = sTmp & "PgDn"
Case %VK_End : sTmp = sTmp & "End"
Case %VK_HOME : sTmp = sTmp & "Home"
Case %VK_LEFT : sTmp = sTmp & "Left Arrow"
Case %VK_UP : sTmp = sTmp & "Up Arrow"
Case %VK_RIGHT : sTmp = sTmp & "Right Arrow"
Case %VK_DOWN : sTmp = sTmp & "Down Arrow"
Case %VK_INSERT : sTmp = sTmp & "Ins"
Case %VK_DELETE : sTmp = sTmp & "Del"
Case %VK_F1
SELECT Case As Long CBLPARAM
Case 0, _ ' no shift keys pressed
%MOD_CONTROL, _ ' Ctrl pressed
%MOD_SHIFT, _ ' Shift pressed
%MOD_CONTROL + %MOD_SHIFT, _ ' Ctrl + Shift pressed
%MOD_ALT + %MOD_CONTROL + %MOD_SHIFT ' Alt + Ctrl + Shift pressed
' NOTE: F1, Ctrl/F1, Shift/F1, Ctrl/Shift/F1 and Alt/Ctrl/Shift/F1 fire the %WM_HELP message
' so let %WM_HELP do the job and exit here
EXIT Function
End SELECT
sTmp = sTmp & "F1"
Case %VK_F2 TO %VK_F10 ' Function keys F2 - F10
sTmp = sTmp & "F" & FORMAT$(CBWPARAM - %VK_F1 + 1)
' numpad keys
Case %VK_NUMPAD0 TO %VK_NUMPAD9
sTmp = sTmp & CHR$(CBWPARAM - %VK_NUMPAD0 + 48) & " (numpad)"
Case %VK_MULTIPLY : sTmp = sTmp & "Multiply (numpad)"
Case %VK_ADD : sTmp = sTmp & "Add (numpad)"
Case %VK_SUBTRACT : sTmp = sTmp & "Substract (numpad)"
Case %VK_DECIMAL : sTmp = sTmp & "Decimal (numpad)"
' End numpad keys
' printable chars
Case 48 TO 57, 65 TO 90 ' 0 - 9, A - Z (ASCII codes)
sTmp = sTmp & CHR$(CBWPARAM)
Case ELSE ' don't process unwanted keys
EXIT Function
End SELECT
CONTROL SET TEXT CBHNDL, 1001, sTmp
End SELECT
End Function
Function KeyBoardProc(ByVal iCode AS INTEGER, ByVal wParam AS DWord, ByVal lParam As Long) AS DWord
Local lShiftMode As Long
IF IsFalse(lParam AND &H80000000) Then ' bit 31 (2^31) NOT set: some key is pressed
IF IsTrue(lParam AND &H20000000) Then lShiftMode = %MOD_ALT ' bit 29 (2^29) set: Alt-key is down
IF (GetAsyncKeyState(%VK_CONTROL) AND &H8000) Then _
lShiftMode = lShiftMode + %MOD_CONTROL ' eventually add Ctrl
IF (GetAsyncKeyState(%VK_SHIFT) AND &H8000) _
Then lShiftMode = lShiftMode + %MOD_SHIFT ' eventually add Shift
PostMessage hDlg, %WM_USER + 101, wParam, lShiftMode ' wParam holds virtual keycode
End IF
Function = CallNextHookEx(ghKeyb, iCode, wParam, lParam) ' proceed
End Function
'gbs_00970
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm