Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to know the pressed keys?

Posted on 1999-07-19
12
Medium Priority
?
230 Views
Last Modified: 2010-04-30
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?
0
Comment
Question by:familine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Expert Comment

by:setiawan
ID: 1525704
Check Keyascii on event KeyPress
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 1525705
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 1525706
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:familine
ID: 1525707
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
 

Author Comment

by:familine
ID: 1525708
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
 
LVL 3

Expert Comment

by:KhollAD
ID: 1525709
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
 

Author Comment

by:familine
ID: 1525710
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
 
LVL 3

Expert Comment

by:KhollAD
ID: 1525711
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
 
LVL 3

Expert Comment

by:KhollAD
ID: 1525712
Correct comment:
.
.
.
In this case we used a keyboard hook, we should use the WH_Keyboard const.  
.
.
.

0
 

Accepted Solution

by:
Julio072399 earned 280 total points
ID: 1525713
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
 

Author Comment

by:familine
ID: 1525714
GOOD!
0
 
LVL 6

Expert Comment

by:setiawan
ID: 1525715
Put into Text2_KeyDown
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question