How to know the pressed keys?

Hi,

We would like to know how to know which keys we have press, and how to block the keys. Ex. How to block the ALT?
familineAsked:
Who is Participating?
 
Julio072399Connect With a Mentor Commented:
In a module:

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Public Const KEYEVENTF_KEYUP = &H2

In a form

Example: This release the CTRL key when you press it.. it simulates a unpressed key

If GetAsyncKeyState(17) <> 0 Then
   keybd_event 17, 0, KEYEVENTF_KEYUP, 0
End If
0
 
setiawanCommented:
Check Keyascii on event KeyPress
0
 
VbmasterCommented:
KeyPress event only occurs for some of the keys like A->Z.. Instead you should use the KeyDown procedure of the form.. be sure to set Form1.Keypreview = True to catch all the keystrokes in your program. Then in your Keydown procedure you can do something like this

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

  If (KeyCode = 18) Then KeyCode = 0

End Sub

(Alt key has keycode 18 i don't know if there is a constant for that key you can use instead).




0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
VbmasterCommented:
To know what buttons are pressed, check the value of Keycode and Shift in the Keydown procedure. Something like this

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If (Shift And vbShiftMask) Then
  'Shift key down
End If

If (Shift and vbCtrlMask) Then
  'Ctrl key down
End If

If (Shift and vbAltMask) Then
  'Alt key down
End If

'Check the keycode for other keys pressed

End Sub

0
 
familineAuthor Commented:
The keypress don't accept the windows key, alt, ctrl,etc...
The keydown works better but "keycode=0" don't work
because the key pressed continues doing the same

0
 
familineAuthor Commented:
The keypress don't accept the windows key, alt, ctrl,etc...
The keydown works better but "keycode=0" don't work
because the key pressed continues doing the same

0
 
KhollADCommented:
You can use a Keyboard Hook to intercept and cancel ALT key.

- paste the code below in your form :

Private Sub Form_Load()

    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyBoardProc, 0&, App.ThreadID)
   
End Sub

Private Sub Form_Unload(Cancel As Integer)

    UnhookWindowsHookEx hHook

End Sub



- paste the code below in a module :

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Public Const WH_KEYBOARD = 2
Public Const HC_ACTION = 0

Private Const VK_MENU = &H12

Public hHook As Long

Public Function KeyBoardProc(ByVal Code As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   
    If Code = HC_ACTION And wParam = VK_MENU Then
        KeyBoardProc = 1
    Else
        KeyBoardProc = CallNextHookEx(hHook, Code, wParam, lParam)
    End If

End Function



0
 
familineAuthor Commented:
That's fine, but as we're novices, can you please explain us a little bit more the code?
We have add it as you told us in the form and the module, but it does nothing (or looks like)... I press ALT+TAB and the message dialog from Windows continue appearing, I thought if this works, when I press ALT+TAB will be the same as TAB only (ALT inhibited)
Also if you can told us:
WE_KEYBOARD (is this the code for the key? Code '2' for ALT?)
HC_ACTION (what's this?)

Thanks
0
 
KhollADCommented:
Hi familine,

I'm Sorry for the not good explained solution.  

Well, a hook is a way to intercept the Windows messages (actions) before they happen for the user, then, we can change the state of this action.  
In this case we used a keyboard hook, we should use the HC_Keyboard const.  
The HC_Action const allows to analyze the moment that the user pressed a key. In this case we asked if the user pressed ALT (VK_Menu - VK = Virtual Key / Menu = Alt Key), then, we canceled this action (KeyBoardProc = 1).  
 
But I am understanding your question now, you want to block the Windows Keystrokes.

Test the code below :

Private Declare Function SystemParametersInfo Lib _
"user32" Alias "SystemParametersInfoA" (ByVal uAction _
As Long, ByVal uParam As Long, ByVal lpvParam As Any, _
ByVal fuWinIni As Long) As Long

- Disable KeyStrokes :

    Dim X As Long
    X = SystemParametersInfo(97, True, CStr(1), 0)

- Enable KeyStrokes :

    Dim X As Long
    X = SystemParametersInfo(97, False, CStr(1), 0)

I hope it help,
Kholl.
0
 
KhollADCommented:
Correct comment:
.
.
.
In this case we used a keyboard hook, we should use the WH_Keyboard const.  
.
.
.

0
 
familineAuthor Commented:
GOOD!
0
 
setiawanCommented:
Put into Text2_KeyDown
0
All Courses

From novice to tech pro — start learning today.