Home All Groups Group Topic Archive Search About

Detecting if Control key is pressed

Author
25 May 2005 3:16 PM
HLong
Hi, I am trying to determine if the user is pressing the control key or
control + shift keys when the user form is loaded, so the proper controls are
make visible.  I am doing this in VBA.  I tried using the apia functions
GetKeyState and GetAsyncKeyState, but I haven't been able to get this to
work.  Can someone give a hand on this or suggest a better approach.  I will
really appreciate it.

Author
25 May 2005 3:22 PM
Bob Butler
"HLong" <HL***@discussions.microsoft.com> wrote in message
news:D71650C2-2ADF-4537-8FC4-05E2FAD73E35@microsoft.com
> Hi, I am trying to determine if the user is pressing the control key
> or control + shift keys when the user form is loaded, so the proper
> controls are make visible.  I am doing this in VBA.  I tried using
> the apia functions GetKeyState and GetAsyncKeyState, but I haven't
> been able to get this to work.  Can someone give a hand on this or
> suggest a better approach.  I will really appreciate it.

In VB this should work:

Private Const VK_CONTROL = &H11
Private Const VK_SHIFT = &H10
Private Declare Function GetKeyState Lib "user32" _
  (ByVal nVirtKey As Long) As Integer

Private Sub Form_Load()
Label1.Caption = "Shift=" & CBool(GetKeyState(VK_SHIFT) And 128) & vbCrLf &
_
  "Ctrl=" & CBool(GetKeyState(VK_CONTROL) And 128)
End Sub

For VBA you may want to take the question to a VBA newsgroup for the
particular application as there may be differences.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
25 May 2005 5:01 PM
HLong
Thanks Bob for your help, it worked fine.  What does the 128 means.  If I try
the function with out the 128, I get either -1, 127, 0, 128.  I don't
understand how this works, would you explain a bit more.  Thanks.

Show quoteHide quote
"Bob Butler" wrote:

> "HLong" <HL***@discussions.microsoft.com> wrote in message
> news:D71650C2-2ADF-4537-8FC4-05E2FAD73E35@microsoft.com
> > Hi, I am trying to determine if the user is pressing the control key
> > or control + shift keys when the user form is loaded, so the proper
> > controls are make visible.  I am doing this in VBA.  I tried using
> > the apia functions GetKeyState and GetAsyncKeyState, but I haven't
> > been able to get this to work.  Can someone give a hand on this or
> > suggest a better approach.  I will really appreciate it.
>
> In VB this should work:
>
> Private Const VK_CONTROL = &H11
> Private Const VK_SHIFT = &H10
> Private Declare Function GetKeyState Lib "user32" _
>   (ByVal nVirtKey As Long) As Integer
>
> Private Sub Form_Load()
> Label1.Caption = "Shift=" & CBool(GetKeyState(VK_SHIFT) And 128) & vbCrLf &
> _
>   "Ctrl=" & CBool(GetKeyState(VK_CONTROL) And 128)
> End Sub
>
> For VBA you may want to take the question to a VBA newsgroup for the
> particular application as there may be differences.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
>
Author
25 May 2005 5:58 PM
Karl E. Peterson
HLong wrote:
> Thanks Bob for your help, it worked fine.  What does the 128 means.
> If I try the function with out the 128, I get either -1, 127, 0, 128.
> I don't understand how this works, would you explain a bit more.

The return value from GetKeyState is encoded into bit fields.  Each bit is
significant.  The high-order bit of the low word is the one that indicates the key
you asked for is pressed down.  If you'd like to learn more about bit manipulation,
you may find http://vb.mvps.org/samples/Twiddle of interest.
--
Working Without a .NET?
http://classicvb.org/petition



Show quoteHide quote
> "Bob Butler" wrote:
>
>> "HLong" <HL***@discussions.microsoft.com> wrote in message
>> news:D71650C2-2ADF-4537-8FC4-05E2FAD73E35@microsoft.com
>>> Hi, I am trying to determine if the user is pressing the control key
>>> or control + shift keys when the user form is loaded, so the proper
>>> controls are make visible.  I am doing this in VBA.  I tried using
>>> the apia functions GetKeyState and GetAsyncKeyState, but I haven't
>>> been able to get this to work.  Can someone give a hand on this or
>>> suggest a better approach.  I will really appreciate it.
>>
>> In VB this should work:
>>
>> Private Const VK_CONTROL = &H11
>> Private Const VK_SHIFT = &H10
>> Private Declare Function GetKeyState Lib "user32" _
>>   (ByVal nVirtKey As Long) As Integer
>>
>> Private Sub Form_Load()
>> Label1.Caption = "Shift=" & CBool(GetKeyState(VK_SHIFT) And 128) &
>> vbCrLf & _
>>   "Ctrl=" & CBool(GetKeyState(VK_CONTROL) And 128)
>> End Sub
>>
>> For VBA you may want to take the question to a VBA newsgroup for the
>> particular application as there may be differences.
>>
>> --
>> Reply to the group so all can participate
>> VB.Net: "Fool me once..."