Programmatically Press Key - How to do it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShizzMasteR
    New Member
    • Apr 2007
    • 5

    Programmatically Press Key - How to do it?

    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 programmaticall y pressing the F1 key continuously. The thing is, I don't know the code to make it press a key for you. Any help?
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by ShizzMasteR
    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 programmaticall y 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.

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by ShizzMasteR
      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 programmaticall y 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)
      Code:
      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

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by SammyB
        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.
        Originally posted by SammyB
        Code:
        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.

        Comment

        • SammyB
          Recognized Expert Contributor
          • Mar 2007
          • 807

          #5
          Originally posted by Killer42
          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.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by SammyB
            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.

            Comment

            • SammyB
              Recognized Expert Contributor
              • Mar 2007
              • 807

              #7
              Originally posted by Killer42
              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.

              Originally posted by Killer42
              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:

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by SammyB
                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.


                Originally posted by SammyB
                No can do. It's blocked as a "shopping" site.

                Originally posted by SammyB
                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.

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  Originally posted by Killer42
                  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!

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

                  Originally posted by Killer42
                  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. :)

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by SammyB
                    Maybe we should just use ones and zeros!
                    Nah. stick with 1's. They're slimmer, so you don't need as much space.

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

                    Originally posted by SammyB
                    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.)

                    Comment

                    • SammyB
                      Recognized Expert Contributor
                      • Mar 2007
                      • 807

                      #11
                      Originally posted by Killer42
                      Um... sorry, what point does that prove? :confused:
                      Obvious: if I didn't teach it, you don't need it! :D

                      Originally posted by Killer42
                      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:

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by SammyB
                        Obvious: if I didn't teach it, you don't need it!
                        Oh, of course. Silly of me. :)

                        Originally posted by SammyB
                        The split function is one of the best things that Microsoft gave us.
                        No argument from me on that point.
                        Originally posted by SammyB
                        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.)

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          Originally posted by Killer42
                          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.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by SammyB
                            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...

                            Comment

                            • malligarjunank
                              New Member
                              • Apr 2007
                              • 10

                              #15
                              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.keydownev entargs(keys.F1 )
                              call formname_keydow n(sender,evt)

                              Comment

                              Working...