By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Programmatically Press Key - How to do it?

P: 5
I'm using Visual Basic 2005 Express Edition. I'm attempting to make a program that, after a button on the form (button1 or whatever I name it), the program will start programmatically pressing the F1 key continuously. The thing is, I don't know the code to make it press a key for you. Any help?
Apr 20 '07 #1
Share this Question
Share on Google+
15 Replies


Expert 5K+
P: 8,434
I'm using Visual Basic 2005 Express Edition. I'm attempting to make a program that, after a button on the form (button1 or whatever I name it), the program will start programmatically pressing the F1 key continuously. The thing is, I don't know the code to make it press a key for you. Any help?
In VB6 it was SendKeys. You could try looking in the upgrade info for things that have changed.
Apr 21 '07 #2

SammyB
Expert 100+
P: 807
I'm using Visual Basic 2005 Express Edition. I'm attempting to make a program that, after a button on the form (button1 or whatever I name it), the program will start programmatically pressing the F1 key continuously. The thing is, I don't know the code to make it press a key for you. Any help?
It survived. :( (I think that using it indicates poor design)
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.SendKeys.Send("{F1}")
Don't ask about the braces: who designs this VB stuff?
Don't mind the ravings of an old man. ;o)>>> For more info, see the help file, http://msdn2.microsoft.com/en-us/lib...keys.send.aspx
Apr 21 '07 #3

Expert 5K+
P: 8,434
It survived. :( (I think that using it indicates poor design)
I disagree. You might just as well say the same about any statement. It depends how you use it.
Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.SendKeys.Send("{F1}")
Don't ask about the braces: who designs this VB stuff?
Don't mind the ravings of an old man. ;o)>>> For more info, see the help file, http://msdn2.microsoft.com/en-us/lib...keys.send.aspx
You have a problem with the braces? Why? What would you suggest instead? VB has to have some way to know you didn't want to send an "F" key followed by a "1" key.
Apr 22 '07 #4

SammyB
Expert 100+
P: 807
I disagree. You might just as well say the same about any statement. It depends how you use it.You have a problem with the braces? Why? What would you suggest instead? VB has to have some way to know you didn't want to send an "F" key followed by a "1" key.
A better reference for SendKeys is http://msdn2.microsoft.com/en-us/lib...48(vs.80).aspx.

Re braces: An enum seems to me to be better: in VB6, we have vbKeyF1, and in DotNet we have System.Windows.Forms.Keys.F1, so why add another language feature.
Apr 23 '07 #5

Expert 5K+
P: 8,434
Re braces: An enum seems to me to be better: in VB6, we have vbKeyF1, and in DotNet we have System.Windows.Forms.Keys.F1, so why add another language feature.
So what you're saying is that setting up a string as
StrThingy = "ABC" & vbKeyF1
is simpler than
StrThingy = "ABC{F1}"

I still don't agree.

It would be nice if they could be used interchangeably, I suppose. Presumably there was some reason why they wanted to use only normal printable characters in SendKeys. Also, I'm not sure VB had anything to do with it - it may be due to other constraints.
Apr 23 '07 #6

SammyB
Expert 100+
P: 807
So what you're saying is that setting up a string as
StrThingy = "ABC" & vbKeyF1
is simpler than
StrThingy = "ABC{F1}"
I wouldn't say simpler. I just don't see a reason for making the language syntax more complicated when "ABC" & vbKeyF1 was already in place. Plus my eyes are so bad that I cannot tell { from (, so I do not like braces anywhere.

I still don't agree.
See http://despair.com/compromise.html ;)

Also, I'm not sure VB had anything to do with it - it may be due to other constraints.
Yea, it's a Net Framework thing. Works in C# also. :rolleyes:
Apr 23 '07 #7

Expert 5K+
P: 8,434
I wouldn't say simpler. I just don't see a reason for making the language syntax more complicated when "ABC" & vbKeyF1 was already in place. Plus my eyes are so bad that I cannot tell { from (, so I do not like braces anywhere.
Well, obviously the use of any similar-looking characters should be banned in all cases. So, no more "O" or "0", and so on. Wow, this is going to be hard to get used to... :p

Or perhaps you should use a bigger font or something.


See http://despair.com/compromise.html
No can do. It's blocked as a "shopping" site.

Yea, it's a Net Framework thing. Works in C# also.
:confused: It's been around since years before the .Net framework was even thought of.
Apr 23 '07 #8

SammyB
Expert 100+
P: 807
Well, obviously the use of any similar-looking characters should be banned in all cases. So, no more "O" or "0", and so on. Wow, this is going to be hard to get used to... :p
Maybe we should just use ones and zeros!

"shopping" site.
You've got to find a computer to get there. You'll love it!

It's been around since years before the .Net framework was even thought of.
Ah, this proves my point! I've taught begining & advanced VB6 programming and never heard of it until I read the help file that started this derailing. :)
Apr 24 '07 #9

Expert 5K+
P: 8,434
Maybe we should just use ones and zeros!
Nah. stick with 1's. They're slimmer, so you don't need as much space.

You've got to find a computer to get there. You'll love it!
Will have a look at home if I remember.

Ah, this proves my point! I've taught begining & advanced VB6 programming and never heard of it until I read the help file that started this derailing.
Um... sorry, what point does that prove? :confused:

In the last couple of months I've come across lots of features of VB6 that I had been unaware of for years - does that prove anything about them? (Example: the Replace and Split functions.)
Apr 24 '07 #10

SammyB
Expert 100+
P: 807
Um... sorry, what point does that prove? :confused:
Obvious: if I didn't teach it, you don't need it! :D

Split functions.
The split function is one of the best things that Microsoft gave us. BTW, if s = "This is cool", did you know you can use Split(s)(1) to get the "is"? :cool:
Apr 24 '07 #11

Expert 5K+
P: 8,434
Obvious: if I didn't teach it, you don't need it!
Oh, of course. Silly of me. :)

The split function is one of the best things that Microsoft gave us.
No argument from me on that point.
BTW, if s = "This is cool", did you know you can use Split(s)(1) to get the "is"?
I did, but only since I encountered it on TSDN a month or three ago. (Of course, in practice it would probably take me a couple of attempts to hit the right word. I would initially expect (1) to return "This" then have to adjust my code. I hate zero-based arrays.)

Wish I had known years ago. I have written tons of code to do the work that Split and Replace do. Not that it hasn't been fun, of course. :) After all, what other reason is there for programming?

(By the way, zero-based arrays do have one or two useful purposes. For something like a sort, where you need to swap elements around, the "zeroth" element is handy as a temporary bucket to hold them. Also it's sometimes handy as a place to store metadata related to the array.)
Apr 24 '07 #12

SammyB
Expert 100+
P: 807
I hate zero-based arrays.
Allowing anything but one-based arrays was one of the worst things that Microsoft did. But, the biggest irritation is their inconsistancy: you never know/can remember if an array is zero or one based. DotNet is infinitely more consistent: I only remember one time when I said that something (forgotten what) was inconsistant.
Apr 24 '07 #13

Expert 5K+
P: 8,434
Allowing anything but one-based arrays was one of the worst things that Microsoft did. But, the biggest irritation is their inconsistancy: you never know/can remember if an array is zero or one based. DotNet is infinitely more consistent: I only remember one time when I said that something (forgotten what) was inconsistant.
I just don't rely on the default. For instance, I would never define an array as MyArray(10). It would be MyArray(1 To 10). At least that way it's spelled out for you, right there.

Hm... I wonder whether Option Base still works...
Apr 24 '07 #14

P: 10
1. The form's KeyPreview set to be true.
2. In form Keydown event for F1 write what u want to Perform.
3. Then in Button Click event write BelowCode

Dim evt as new System.windows.forms.keydowneventargs(keys.F1)
call formname_keydown(sender,evt)
Apr 24 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.