I need help writing a keyboard event handler in VBA.

Programming and macros
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

I need help writing a keyboard event handler in VBA.

Unread post by loeb »

I need help writing a keyboard event handler in VBA.

I'm trying to write a macro that will interact with the mouse and keyboard. I found a GREAT forum post where Josh Brady posted some code that works great with the mouse using GetMouse. In this example, if the mouse is clicked, a dialog comes up with the mouse position (X, Y). I'd like to add a listener to it where it listens for a keyboard click as well. For example, if a key is pressed and that key is ESC, then it will end the macro.

Here's a link to the post where Josh Brady's shared his code. It's a MUST READ for anyone who is trying to learn how to handle the mouse.

"Help with mouse clicks"
https://r1132100503382-eu1-3dswym.3dexp ... mDUWtXrSog
User avatar
gupta9665
Posts: 361
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 384
x 417

Re: I need help writing a keyboard event handler in VBA.

Unread post by gupta9665 »

SendKeys should be the key you looking for. Check codes by @Josh here https://r1132100503382-eu1-3dswym.3dexp ... JEaLiC692g
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: I need help writing a keyboard event handler in VBA.

Unread post by loeb »

gupta9665 wrote: Sun Mar 20, 2022 1:35 am SendKeys should be the key you looking for. Check codes by @Josh here https://r1132100503382-eu1-3dswym.3dexp ... JEaLiC692g
Deepak,
Thank you for your response. The Microsoft VBA reference says that SendKeys "Sends one or more keystrokes to the active window as if typed at the keyboard."

https://docs.microsoft.com/en-us/office ... -statement

I'm not sure how I could use "SendKeys" to LISTEN for keystrokes.
User avatar
gupta9665
Posts: 361
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 384
x 417

Re: I need help writing a keyboard event handler in VBA.

Unread post by gupta9665 »

Sorry, I went the other way round. Check the macro codes here https://wellsr.com/vba/2017/excel/GetAs ... s-pressed/
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
loeb
Posts: 47
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 35
x 8

Re: I need help writing a keyboard event handler in VBA.

Unread post by loeb »

gupta9665 wrote: Mon Mar 21, 2022 2:23 am Sorry, I went the other way round. Check the macro codes here https://wellsr.com/vba/2017/excel/GetAs ... s-pressed/
Nice! I'll check that out.
Post Reply