I'm trying set up a user form where I can assign shortcut keys to various
subroutines.

I know how to do the keybinding (assign the shortcut keys) once I get the
keys in the text box -- but am totally stumped on how to "capture" the
keystrokes the user might enter in the form's text boxes.

Unfortunately I'm pretty much a newbie with programming in general and VBA
in particular...

Am working on trying to get something like the following to work, but even
if I can get this running right, it seems I'd need to specify every possible
key combo. I'm sure that can't be the most reasonable approach...

I'd really appreciate any advice or help you can offer!

Private Sub Texbox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
' If the 'Alt' and 'E' keys are pressed,
If e.Alt And e.KeyCode = Keys.e Then
Texbox1.Value = "Alt + E"
Else
Texbox1.Value = "nope"
End If
End Sub

Re: capture keydown/press events in a user form by Perry

Perry
Fri Feb 02 16:45:04 CST 2007

Try below code...

Private Sub TextBox1_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Static bALTPressedFirst As Boolean
If bALTPressedFirst Then
If KeyCode = 69 Then
MsgBox "ALT E pressed "
bALTPressedFirst = False
End If
End If
bALTPressedFirst = (KeyCode = 18)
End Sub

Krgrds,
Perry



"Lisa" <Lisa@discussions.microsoft.com> wrote in message
news:AEE41E2F-D938-4152-9CE0-103B46DD4B1C@microsoft.com...
> I'm trying set up a user form where I can assign shortcut keys to various
> subroutines.
>
> I know how to do the keybinding (assign the shortcut keys) once I get the
> keys in the text box -- but am totally stumped on how to "capture" the
> keystrokes the user might enter in the form's text boxes.
>
> Unfortunately I'm pretty much a newbie with programming in general and VBA
> in particular...
>
> Am working on trying to get something like the following to work, but even
> if I can get this running right, it seems I'd need to specify every
> possible
> key combo. I'm sure that can't be the most reasonable approach...
>
> I'd really appreciate any advice or help you can offer!
>
> Private Sub Texbox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
> Shift As Integer)
> ' If the 'Alt' and 'E' keys are pressed,
> If e.Alt And e.KeyCode = Keys.e Then
> Texbox1.Value = "Alt + E"
> Else
> Texbox1.Value = "nope"
> End If
> End Sub


Re: capture keydown/press events in a user form by Lisa

Lisa
Fri Feb 02 20:38:01 CST 2007

Thanks, Perry. That works perfectly for determining if Alt+E is pressed. I'm
still stumped as to how I can set this up to cover all reasonable combos I
might want to use as shortcut keys -- without coding each individually.

Will play with this when I'm a bit more perky ;-)
Thanks again for your reply.

"Perry" wrote:

> Try below code...
>
> Private Sub TextBox1_KeyDown(ByVal _
> KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
>
> Static bALTPressedFirst As Boolean
> If bALTPressedFirst Then
> If KeyCode = 69 Then
> MsgBox "ALT E pressed "
> bALTPressedFirst = False
> End If
> End If
> bALTPressedFirst = (KeyCode = 18)
> End Sub
>
> Krgrds,
> Perry
>
>
>
> "Lisa" <Lisa@discussions.microsoft.com> wrote in message
> news:AEE41E2F-D938-4152-9CE0-103B46DD4B1C@microsoft.com...
> > I'm trying set up a user form where I can assign shortcut keys to various
> > subroutines.
> >
> > I know how to do the keybinding (assign the shortcut keys) once I get the
> > keys in the text box -- but am totally stumped on how to "capture" the
> > keystrokes the user might enter in the form's text boxes.
> >
> > Unfortunately I'm pretty much a newbie with programming in general and VBA
> > in particular...
> >
> > Am working on trying to get something like the following to work, but even
> > if I can get this running right, it seems I'd need to specify every
> > possible
> > key combo. I'm sure that can't be the most reasonable approach...
> >
> > I'd really appreciate any advice or help you can offer!
> >
> > Private Sub Texbox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
> > Shift As Integer)
> > ' If the 'Alt' and 'E' keys are pressed,
> > If e.Alt And e.KeyCode = Keys.e Then
> > Texbox1.Value = "Alt + E"
> > Else
> > Texbox1.Value = "nope"
> > End If
> > End Sub
>
>

Re: capture keydown/press events in a user form by Jean-Guy

Jean-Guy
Fri Feb 02 23:46:01 CST 2007

Perry was telling us:
Perry nous racontait que :

> Try below code...
>
> Private Sub TextBox1_KeyDown(ByVal _
> KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
>
> Static bALTPressedFirst As Boolean
> If bALTPressedFirst Then
> If KeyCode = 69 Then
> MsgBox "ALT E pressed "
> bALTPressedFirst = False
> End If
> End If
> bALTPressedFirst = (KeyCode = 18)
> End Sub
>

Perry,

Can you explain how bALTPressedFirst gets changed to True when the ALT key
is used?

Thanks.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: capture keydown/press events in a user form by Perry

Perry
Sat Feb 03 07:17:31 CST 2007

> Can you explain how bALTPressedFirst gets changed to True when the ALT key
> is used?

The code always passes last line of code
bALTPressedFirst = (KeyCode = 18)

Have y tried it?
Apparently, it works for the OP.

Krgrds,
Perry

"Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote in message
news:eUZ8Ya1RHHA.1200@TK2MSFTNGP02.phx.gbl...
> Perry was telling us:
> Perry nous racontait que :
>
>> Try below code...
>>
>> Private Sub TextBox1_KeyDown(ByVal _
>> KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
>>
>> Static bALTPressedFirst As Boolean
>> If bALTPressedFirst Then
>> If KeyCode = 69 Then
>> MsgBox "ALT E pressed "
>> bALTPressedFirst = False
>> End If
>> End If
>> bALTPressedFirst = (KeyCode = 18)
>> End Sub
>>
>
> Perry,
>
> Can you explain how bALTPressedFirst gets changed to True when the ALT key
> is used?
>
> Thanks.
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>


Re: capture keydown/press events in a user form by Jean-Guy

Jean-Guy
Mon Feb 05 09:39:43 CST 2007

Perry was telling us:
Perry nous racontait que :

>> Can you explain how bALTPressedFirst gets changed to True when the
>> ALT key is used?
>
> The code always passes last line of code
> bALTPressedFirst = (KeyCode = 18)
>
> Have y tried it?
> Apparently, it works for the OP.


Of course it works.. I did not doubt the validity of your code, I was
genuinely interested in understanding the behind the scene stuff...

Before the code gets to
bALTPressedFirst = (KeyCode = 18)
if ALT was not pressed, then
If bALTPressedFirst
is false. If Alt was pressed it is true.
Even the first time you use the code.

I do not understand the relationship between what seems to be a variable and
the fact that its value is affected by a keyboard press.

Thanks for enlightening me!

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: capture keydown/press events in a user form by Perry

Perry
Mon Feb 05 13:53:47 CST 2007

> Of course it works.. I did not doubt the validity of your code, I was
> genuinely interested in understanding the behind the scene stuff...

:-)
No probs if you were, Jean-Guy.

My point: it worked for OP, and I've come to believe due to being in the
bizz
for quite a time now: elegant/ideal solutions don't always matter ... (you
know)
Having looked at the code, after I posted, there WAS one line questionable.
Indicated in below sequence...

Krgrds,
Perry


Static bALTPressedFirst As Boolean
If bALTPressedFirst Then
If KeyCode = 69 Then
MsgBox "ALT E pressed "
bALTPressedFirst = False '<<< obsolete
End If
End If




"Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote in message
news:%23U0sevTSHHA.1200@TK2MSFTNGP02.phx.gbl...
> Perry was telling us:
> Perry nous racontait que :
>
>>> Can you explain how bALTPressedFirst gets changed to True when the
>>> ALT key is used?
>>
>> The code always passes last line of code
>> bALTPressedFirst = (KeyCode = 18)
>>
>> Have y tried it?
>> Apparently, it works for the OP.
>
>
> Of course it works.. I did not doubt the validity of your code, I was
> genuinely interested in understanding the behind the scene stuff...
>
> Before the code gets to
> bALTPressedFirst = (KeyCode = 18)
> if ALT was not pressed, then
> If bALTPressedFirst
> is false. If Alt was pressed it is true.
> Even the first time you use the code.
>
> I do not understand the relationship between what seems to be a variable
> and the fact that its value is affected by a keyboard press.
>
> Thanks for enlightening me!
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>


Re: capture keydown/press events in a user form by Jean-Guy

Jean-Guy
Tue Feb 06 10:23:45 CST 2007

Perry was telling us:
Perry nous racontait que :

>> Of course it works.. I did not doubt the validity of your code, I was
>> genuinely interested in understanding the behind the scene stuff...
>
> :-)
> No probs if you were, Jean-Guy.
>
> My point: it worked for OP, and I've come to believe due to being in
> the bizz
> for quite a time now: elegant/ideal solutions don't always matter ...
> (you know)
> Having looked at the code, after I posted, there WAS one line
> questionable. Indicated in below sequence...
>
> Krgrds,
> Perry
>
>
> Static bALTPressedFirst As Boolean
> If bALTPressedFirst Then
> If KeyCode = 69 Then
> MsgBox "ALT E pressed "
> bALTPressedFirst = False '<<< obsolete
> End If
> End If

Perry, I think you do not understand my posts in this thread... :-)

I know the code works ( I tested it) and I could leave it at that.
But being who I am, I can't let it go until I understand what is going on...
I need/want to understand HOW it works, .

I want to know how a keypress (Using ALT or not) affects the value of a
variable in a Sub (bALTPressedFirst = True or False).
Is bALTPressedFirst some sort of VBA constant?
What If I wanted to test for CTRL being pressed or not?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: capture keydown/press events in a user form by Perry

Perry
Tue Feb 06 15:34:22 CST 2007

Oh did I understand yr contributions, I sure did :-)

I just thought the code was more or less self explanatory but for other
readers, will give
some addtl explanation.

> Is bALTPressedFirst some sort of VBA constant?

Static bALTPressedFirst As Boolean
expl: Declared static; keeps value next time the event fires.

bALTPressedFirst = (KeyCode = 18)
expl: Last line in eventprocedure determines whether ALT (KeyCode = 18) was
pressed.

So next time event fires and ALT was pressed (static variable
bALTPressedFirst = True )
only thing needs to be done is catch keycode for [e] has.
If [e] was pressed, the last line will reset static variable
bALTPressedFirst to False....etc.

Can't catch Keycode for CTRL; there's no system callback for CTRL.

Krgrds,
Perry

"Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote in message
news:O4wtwsgSHHA.3500@TK2MSFTNGP05.phx.gbl...
> Perry was telling us:
> Perry nous racontait que :
>
>>> Of course it works.. I did not doubt the validity of your code, I was
>>> genuinely interested in understanding the behind the scene stuff...
>>
>> :-)
>> No probs if you were, Jean-Guy.
>>
>> My point: it worked for OP, and I've come to believe due to being in
>> the bizz
>> for quite a time now: elegant/ideal solutions don't always matter ...
>> (you know)
>> Having looked at the code, after I posted, there WAS one line
>> questionable. Indicated in below sequence...
>>
>> Krgrds,
>> Perry
>>
>>
>> Static bALTPressedFirst As Boolean
>> If bALTPressedFirst Then
>> If KeyCode = 69 Then
>> MsgBox "ALT E pressed "
>> bALTPressedFirst = False '<<< obsolete
>> End If
>> End If
>
> Perry, I think you do not understand my posts in this thread... :-)
>
> I know the code works ( I tested it) and I could leave it at that.
> But being who I am, I can't let it go until I understand what is going
> on... I need/want to understand HOW it works, .
>
> I want to know how a keypress (Using ALT or not) affects the value of a
> variable in a Sub (bALTPressedFirst = True or False).
> Is bALTPressedFirst some sort of VBA constant?
> What If I wanted to test for CTRL being pressed or not?
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>


Re: capture keydown/press events in a user form by Jean-Guy

Jean-Guy
Tue Feb 06 22:03:29 CST 2007

Perry was telling us:
Perry nous racontait que :

> Oh did I understand yr contributions, I sure did :-)
>
> I just thought the code was more or less self explanatory but for
> other readers, will give
> some addtl explanation.
>
>> Is bALTPressedFirst some sort of VBA constant?
>
> Static bALTPressedFirst As Boolean
> expl: Declared static; keeps value next time the event fires.
>
> bALTPressedFirst = (KeyCode = 18)
> expl: Last line in eventprocedure determines whether ALT (KeyCode =
> 18) was pressed.
>
> So next time event fires and ALT was pressed (static variable
> bALTPressedFirst = True )
> only thing needs to be done is catch keycode for [e] has.
> If [e] was pressed, the last line will reset static variable
> bALTPressedFirst to False....etc.
>
> Can't catch Keycode for CTRL; there's no system callback for CTRL.

Thank you, I finally got it...

I was under the impression that the code would fire after ALT-E was pressed,
but in fact, if you do ALT-E the code is fired twice... Once for the ALT
press and again for the E press..., but, since the press on E is done while
the ALT key is still pressed, I somehow got the impression that the code
would fire after both were released... ( I guess I did not connect with the
fat that this is a KeyDown event, not a KeyUp one!) This is why I did not
understand how you could catch the ALT key press.

But now that I understand that they are two separate key presses as far as
the code is concerned, I totally get it!

Thanks for putting up with my thick-headedness!

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: capture keydown/press events in a user form by Perry

Perry
Wed Feb 07 11:22:37 CST 2007

> Thanks for putting up with my thick-headedness!
??

On dit en Dunquerque: Pas de billes!!
(in case the Dunquerqian translation didn't come through: No prob!)
Salut!

--
Krgrds,
Perry

System: Vista/Office Ultimate
"Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote in message
news:u$tbxzmSHHA.4404@TK2MSFTNGP03.phx.gbl...
> Perry was telling us:
> Perry nous racontait que :
>
>> Oh did I understand yr contributions, I sure did :-)
>>
>> I just thought the code was more or less self explanatory but for
>> other readers, will give
>> some addtl explanation.
>>
>>> Is bALTPressedFirst some sort of VBA constant?
>>
>> Static bALTPressedFirst As Boolean
>> expl: Declared static; keeps value next time the event fires.
>>
>> bALTPressedFirst = (KeyCode = 18)
>> expl: Last line in eventprocedure determines whether ALT (KeyCode =
>> 18) was pressed.
>>
>> So next time event fires and ALT was pressed (static variable
>> bALTPressedFirst = True )
>> only thing needs to be done is catch keycode for [e] has.
>> If [e] was pressed, the last line will reset static variable
>> bALTPressedFirst to False....etc.
>>
>> Can't catch Keycode for CTRL; there's no system callback for CTRL.
>
> Thank you, I finally got it...
>
> I was under the impression that the code would fire after ALT-E was
> pressed, but in fact, if you do ALT-E the code is fired twice... Once for
> the ALT press and again for the E press..., but, since the press on E is
> done while the ALT key is still pressed, I somehow got the impression that
> the code would fire after both were released... ( I guess I did not
> connect with the fat that this is a KeyDown event, not a KeyUp one!) This
> is why I did not understand how you could catch the ALT key press.
>
> But now that I understand that they are two separate key presses as far as
> the code is concerned, I totally get it!
>
> Thanks for putting up with my thick-headedness!
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>