key Press on Forms Button

  • Thread starter Thread starter Todd Huttenstine
  • Start date Start date
T

Todd Huttenstine

Ok This is slightly different. Here I am using forms
buttons, not commandbuttons. I have a forms button
called "Button 1" A macro is assigned to it called
macro "Macro1".

The macro contains the following 2 lines of code:
Range("A5").Select
Selection.Copy

I want to be able to determine when I click on this button
if I was holding down the shift key or not. Now I have
about 100 forms buttons like this on my worksheet and they
all have the same 2 lines of code, just referencing
different cells. What I am trying to get at is this... I
want to be able to hold shift down when I click on each of
these buttons and if the program detects I am holding
shift, it run a different code.

How do I do this?

Thanx
Todd Huttenstine
 
Todd,

Here is an example

Declare Function GetKeyState Lib "user32" (ByVal fnKey As Long) As Integer

Const vkShift As Integer = &H10

Sub TestButton()

If GetKeyState(vkShift) < 0 Then
MsgBox "Shift pressed"
Else
MsgBox "Normal"
End If
End Sub



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
This is always coming up normal

-----Original Message-----
Todd,

Here is an example

Declare Function GetKeyState Lib "user32" (ByVal fnKey As Long) As Integer

Const vkShift As Integer = &H10

Sub TestButton()

If GetKeyState(vkShift) < 0 Then
MsgBox "Shift pressed"
Else
MsgBox "Normal"
End If
End Sub



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)




.
 
I'm kindof a newbie and I was wondering why you need the
line:

Const vkShift As Integer = &H10

I looked at Microsofts API info for the GetKeyState
function. In reading that, I cannot see where I would
have come to the undertanding that I would need the extra
line. It sounds to me like vkShift is a virtual KeyCode
constant that windows knows. I would relly appreciate
some mentoring on this. I would like to use more API
calls, but have difficulty understanding how to use them
based on the info Microsoft provides...The API info is at:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/winui/winui/windowsuserinterface/userinput/keyboardinput
/keyboardinputreference/keyboardinputfunctions/getkeystate.
asp
 
It works for me Todd. Excuse me if this is blindingly obvious, but the shift
key has to be held down in conjunction with clicking the button.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
You can also use this technique on the Control toolbox buttons.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Hawk,

You don't NEED that line, as you can pass the value &H10 to the API directly

If GetKeyState(&H10) < 0 Then

Often with APIs there are a set of values that can be passed to the API, and
you see these declared as constants, as a maintenance and understanding aid.

As to how I knew it was &H10, well I guess that is just down to learning and
experience. For APIs I use 3 main references. The first is Allapi's API
Guide (download from www.allapi.net -great site), the second is Dan
Appleman's Visual Basic Programmer's Guide to the Win32 API, and when
starting out, Jason Bock's Win32 API Tutorial.

Plus of course there are thousands of examples out on the net.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
I put:
GetKeyState()
in the click event of the commandbutton but what should I
put in the ()?

What I put the following in a the commandbutton click
event by itself it works:

Const vkShift As Integer = &H10
If GetKeyState(vkShift) < 0 Then
MsgBox "Shift pressed"
Else
MsgBox "Normal"
End If

Cant figure it out, maybe its something very simple I
missing...
 
And also I cant find anything in VBHelp about vkShift or
fnKey.

Do I need a different version?
 
and you won't. vkSHft is just a name assigned to the constant, and fnKey is
just the name of the argument. They could be Joe and Bill for all the
difference it makes, it is just a handle.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top