capture keydown/press events in a user form

L

Lisa

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
 
P

Perry

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
 
L

Lisa

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.
 
J

Jean-Guy Marcil

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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Perry

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

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

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

Krgrds,
Perry
 
J

Jean-Guy Marcil

Perry was telling us:
Perry nous racontait que :
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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Perry

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
 
J

Jean-Guy Marcil

Perry was telling us:
Perry nous racontait que :
:)
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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Perry

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
 
J

Jean-Guy Marcil

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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
P

Perry

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 said:
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
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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

Top