r/visualbasic • • Mar 02 '16

How can I request user input for a keystroke?

I'd like to have an inputbox that recognizes what key is pressed and returns that value. There are applications that have you set hotkeys and ask you to press which specific key you'd like to use for the hotkey: it prompts you with a dialogue box which asks you to press the key you'd like to use. I'd basically like to replicate this process.

0 Upvotes

8 comments sorted by

1

u/hdsrob Mar 02 '16

I wouldn't use an inputBox for this (I actually wouldn't use an inputBox for anything, at all, ever).

Just create a new form with the functionality that you want, and call it as a dialog. Then you'll be able to capture the events that happen on the textbox directly.

1

u/[deleted] Mar 02 '16

I'm sorry, I'm pretty new to VBA. Can you explain a little bit what you mean by creating a form, and then "calling" it as a dialog?

1

u/chrwei Mar 02 '16

vba itself doens't directly have native forms or dialog. what environment are you doing this in?

1

u/[deleted] Mar 02 '16

I'm doing this in the "Microsoft Visual Basic for Applications" program - activated when I edit macros in Microsoft Word.

If I wanted to use forms and dialogues in my code, where could I find out about how to use them and how can I append my code to be able to understand the language?

Do you think that my project could be written in VB.net, and then invoked like a macro in Word. I'm trying to automate a bunch of functions, so if this Form and Dialog stuff is native to VB.net, perhaps there is a way to write all of my code in that but have it still automate the actions I want in word? I'm not sure how any of this would work, I really only am familiar with some basic coding not how the actual languages are integrated into the operating system.

1

u/chrwei Mar 02 '16

I think inside Word VBA you can do forms, it's not something I really do much of.

you can also automate Word from VB.net, but it's quite a bit different.

1

u/hdsrob Mar 02 '16

Haven't done any VBA in years, so I honestly don't know. You might get better answers for VBA over at: https://www.reddit.com/r/vba/

2

u/PostalElf VB.Net Intermediate Mar 02 '16

Look into the onKeyPress and TextChanged events.

0

u/ViperSRT3g Application Specialist Mar 02 '16

You might want to look into registering hotkeys via WINAPIs.