finding if certains keys are held down on click

bri822

Registered User.
Local time
Today, 07:19
Joined
May 23, 2002
Messages
31
I have tried to search the message board for this problem but I am not finding any information. My problem is relatively simple. When a user click and button, I want it to exit the application. If the user is holding down a certain key (almost any key would work) I want it to open up an "sercret" screen. I have not found any way so far to tell if a certain key is being held down. I am not even sure if this is possible, but if it is I would appreciate any information on it. Thank you!

Brian
 
granted, that is an event. But the event I want to check on is the "on_click" event. So when someone clicks a button it checks to see if someone is holding down a key. I don't want to check is someone hit a key, until they actually click the button. sorry for being ambigous. Any information much appreciated! Thanks,

Brian
 
Try this.

Dim intShiftDown As Integer, intAltDown As Integer
Dim intCtrlDown As Integer ', Shift As Integer

Private Sub Form_Load()
Me.KeyPreview = True
End Sub

Private Sub cmdKeyDown_Click()
If intShiftDown Then MsgBox "You pressed the SHIFT key."
If intAltDown Then MsgBox "You pressed the ALT key."
If intCtrlDown Then MsgBox "You pressed the CTRL key."
intCtrlDown = 0
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Use bit masks to determine which key was pressed.
intShiftDown = (Shift And acShiftMask) > 0
intAltDown = (Shift And acAltMask) > 0
intCtrlDown = (Shift And acCtrlMask) > 0

End Sub
 
Yes, the OnClick is eventually where you will capture the state of the keyboard, but it all must start in the KeyDown event.

Code:
Option Compare Database
Option Explicit
Dim SpecKey As Boolean

Private Sub Command4_Click()

    If SpecKey Then MsgBox "Spec Message"
    SpecKey = False
    
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If KeyCode = vbKeyD Then
        SpecKey = True
    Else
        SpecKey = False
    End If
    
End Sub

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

    SpecKey = False

End Sub

Private Sub Form_Load()

    Me.StopCal.Value = Date
    Me.Refresh
    Me.KeyPreview = True
    
End Sub

I try not to just blurt out the answers. ;)
 
Shift Argument & Bitwise Math

Shift Argument & Bitwise Math

I have a need to constrain the use of special keys in my program. The search for a proper approach has led me to the form's OnKeyDown and OnKeyUp events. Studying various types of code to handle keyboard combinations led me further into the SHIFT argument and the keyboard constants acAltMask, acCrtlMask and acShiftMask.

I am uncertain how this argument is leveraged. My books state that Access uses Bitwise Math to provide a value to SHIFT but I do not understand how this is used.

As best I can tell the following is true in the context of OnKeyDown/UP. There is always a SHIFT argument and this argument contains possible values as follows:

SHIFT = acShiftMask = 1
CONTROL = acCtrlMask = 2
ALT = acAltMask = 4
None Of the Above = 0

Several posts and an example in Stan Leszynski's Access97 Expert Solutions use an assignment statement like this:

BlnCtrl = (Shift And acCtrlMask > 0 'get status of Ctrl Key

And this makes no sense to me.

When I test the values for a key combination such as SHIFT & D, Alt & D or CTRL & D; I always find positive values for the SHIFT argument and also for all of the 3 of the constants regardless of which key combination I use.

I find that the following statement provides clear choices:

If Shift = 1 then msgbox("The Shift key was pressed")
If Shift = 2 then msgbox("The Control key was pressed")
If Shift = 4 then msgbox("The Alt key was pressed").

Consider the following code:

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim blnShift As Boolean 'To test for Shift key
Dim blnShift As Boolean 'To test for Shift key
Debug.Print Shift
If Shift = 2 Then MsgBox "you pressed the Control key"
If Shift = 2 And acCtrlMask > 0 Then MsgBox "you pressed the Control key"
If Shift And acCtrlMask > 0 Then MsgBox ("hi")
If blnShift = (Shift And acCtrlMask) > 0 Then MsgBox "you pressed the Control key"

If Shift = 4 Then MsgBox "you pressed the Alt key"
If Shift = 4 And acAltMask > 0 Then MsgBox "you pressed the Alt key"
If Shift And acAltMask > 0 Then MsgBox ("hi")
If blnShift = (Shift And acAltMask) > 0 Then MsgBox "you pressed the Alt key"


If Shift = 1 Then MsgBox "you pressed the Shift key"
If Shift = 1 And acShiftMask > 0 Then MsgBox "you pressed the Shift key"
If Shift And acShiftMask > 0 Then MsgBox ("hi")
If blnShift = (Shift And acShiftMask) > 0 Then MsgBox "you pressed the Shift key"

On the above, regardless of which beginning key I start with (Ctrl, Alt,Shift) I get 3 "Hi" messages and never does the If blnShift fire.

I can work with the latter but am curious as to the reasoning behind the If BlnCtrl statement I kept running across. I can't help but feel that I am missing the point! Am I?
 
This post relates to disabling special Access key actions from user control.

Continuing to experiment and test, I came up with the code below. It is in the form's KeyDown event.

The code on early inspection works for all but 3 of the case selections. I don't understand WHY it does not for CTRL + PlusSign (vbKeyAdd?), CTRL + MinusSign (vbKeySubtract?) and ALT + F4 (Quit Access).

In addition there are 2 3-key sequences that I cannot trap:
CTRL + SHIFT + PlusSign (add a record) and CTRL + SHIFT + MinusSign (delete the record).

I'd appreciate any comments!
=====================================

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 0 Then 'No special key
Select Case KeyCode
Case vbKeyF12 'F12 key - save as
KeyCode = 0
Case vbKeyPageUp 'Pageup key - next record
KeyCode = 0
Case vbKeyPageDown 'Pagedown key - next record
KeyCode = 0
End Select
ElseIf Shift = 1 Then 'Shift key
Select Case KeyCode
Case vbKeyF4 'F4 key - find or replace
KeyCode = 0
Case vbKeyF12 'F12 key - save current object
KeyCode = 0
Case vbKeySpace 'Spacebar - select record
KeyCode = 0
Case vbKeyReturn 'Enter key - save changes
KeyCode = 0
End Select
ElseIf Shift = 2 Then 'Control key
Select Case KeyCode
Case vbKeyA 'A key - select all records
KeyCode = 0
Case vbKeyF 'F key - display find box
KeyCode = 0
Case vbKeyH 'H key - display replace box
KeyCode = 0
Case vbKeyN 'N key - open a new database
KeyCode = 0
Case vbKeyO 'O key - open existing database
KeyCode = 0
Case vbKeyF4, vbKeyW 'F4 & W keys - close active window
KeyCode = 0
Case vbKeyF6 'cycle between open windows
KeyCode = 0
Case vbKeyAdd '+ sign - add new record
KeyCode = 0
Case vbKeySubtract 'minus sign - delete current record
KeyCode = 0
Case vbKeyPageDown 'next record
KeyCode = 0
Case vbKeyPageUp 'previous record
KeyCode = 0
End Select
ElseIf Shift = 3 Then 'Alt key
Select Case KeyCode
Case vbKeyF2 'F2 key - save as
KeyCode = 0
Case vbKeyF4 'F4 key - close dialogue box, quit access
KeyCode = 0
Case vbKeyH 'H key - display replace box
KeyCode = 0
Case vbKeyN 'N key - open a new database
KeyCode = 0
Case vbKeyO 'O key - open existing database
KeyCode = 0
End Select
End If

End Sub
 
Well, the code in my previous post is working pretty well now. I do not know what made the key-presses I found not working to work. I did steal some code from ghudson for the CTRL + F4 as a separate If Statement:

Select Case KeyCode
Case vbKeyF4
If intAltDown Then
KeyCode = 0
MsgBox "Alt+F4 has been Disabled.", vbExclamation + vbOKOnly, " Disabled Key"
End If
End Select

But the best part was moving the code to a module public function and calling it from all the forms I was concerned with.

The additional bit I learned is that if a form has a subform, the property KeyPreview should be set to YES on both and the call to the function added to both in the OnKeyDown event.

I'll going to let this code cool a bit and see if it works tomorrow!
 

Users who are viewing this thread

Back
Top Bottom