Odd key trapping issue on KeyDown event

  • Thread starter pubdude2003 via AccessMonster.com
  • Start date
P

pubdude2003 via AccessMonster.com

I prepared this bit of code over a year ago and it works fine on my computer.
However I have recently been trying to trap a "~" (tilde) symbol to do a
quick form open and can't seem to get it to work.

Question one is how do I trap the "~" key?

And question two is can anyone explain why this is working? I don't appear to
be trapping the correct keycodes... but I must be, it's working fine on my
machine, Access 2003 in XP
Case 111, 191 'Divide
KeyCode = 0
SendKeys "{tab}"
SendKeys "/"
Case 106 'Multiply
KeyCode = 0
SendKeys "{tab}"
SendKeys "*"
Case 107 'Addition
KeyCode = 0
SendKeys "{tab}"
SendKeys "{+}"
Case 109, 189 'Minus
KeyCode = 0
SendKeys "{tab}"
SendKeys "{-}"

Any help would be appreciated!
 
D

Dirk Goldgar

pubdude2003 via AccessMonster.com said:
I prepared this bit of code over a year ago and it works fine on my
computer. However I have recently been trying to trap a "~" (tilde)
symbol to do a quick form open and can't seem to get it to work.

Question one is how do I trap the "~" key?

And question two is can anyone explain why this is working? I don't
appear to be trapping the correct keycodes... but I must be, it's
working fine on my machine, Access 2003 in XP
Case 111, 191 'Divide
KeyCode = 0
SendKeys "{tab}"
SendKeys "/"
Case 106 'Multiply
KeyCode = 0
SendKeys "{tab}"
SendKeys "*"
Case 107 'Addition
KeyCode = 0
SendKeys "{tab}"
SendKeys "{+}"
Case 109, 189 'Minus
KeyCode = 0
SendKeys "{tab}"
SendKeys "{-}"

Any help would be appreciated!

The key codes are right, though incomplete -- you're missing the shifted
alternatives for addition and multiplication (from the top row of the
main set of keys). The tilde is also a shifted key. To check for the
shifted keys using the KeyDown event (which gives you keyboard scan
codes, not ASCII codes), you have to check the Shift argument that is
passed to the event procedure, to see if the Shift key was also pressed:

Case 192
If (Shift And acShiftMask) > 0 Then
KeyCode = 0
DoCmd.OpenForm ...
End If

If these are all the keys you're checking for, it might be easier to use
the KeyPress event instead of KeyDown. The KeyPress event passes the
ASCII character value, so you don't need to worry about whether the
Shift key was down or not, and you don't have to allow for alternate key
codes that translate to the same character. Of course, you'd have to
change the set of codes you check for, as the ASCII character codes
aren't the same as the key codes.
 
P

pubdude2003 via AccessMonster.com

Thanks very much Dirk, I suspected I wasn't using ascii code but it had been
so long, I couldn't remember my thinking at the time.

Can you direct me to a chart of the keyboard scan codes? Because of the
nature of the form I would prefer them to use the keypad rather thank the
keyboard and would rather not recode the forms involved.
 
D

Dirk Goldgar

pubdude2003 via AccessMonster.com said:
Thanks very much Dirk, I suspected I wasn't using ascii code but it
had been so long, I couldn't remember my thinking at the time.

Can you direct me to a chart of the keyboard scan codes? Because of
the nature of the form I would prefer them to use the keypad rather
thank the keyboard and would rather not recode the forms involved.

In the VB Editor, type "KeyCode Constants" in the "search help" box, or
navigate to it via the (VBA) help contents pane: "Constants/KeyCode
Constants"
 
P

pubdude2003 via AccessMonster.com

of course... silly me, thanks again for the help Dirk, appreciate your
responses
 

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