Adding keyboard keys to an Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lazareth
    New Member
    • Jan 2007
    • 29

    #1

    Adding keyboard keys to an Application

    Hi,

    I am new to VB. I have just written a simple program to help me calculate GST.

    Everything works good but I was wanting to adjust my program so that as well as clicking on the buttons I could also use keyboard keys programmed to each button.

    Does anyone know a simple solution to accomplish this?
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by Lazareth
    Hi,

    I am new to VB. I have just written a simple program to help me calculate GST.

    Everything works good but I was wanting to adjust my program so that as well as clicking on the buttons I could also use keyboard keys programmed to each button.

    Does anyone know a simple solution to accomplish this?
    make the form keypreview property = true

    and call the command button method with the help of keyup event of the form

    Comment

    • aaryan
      New Member
      • Nov 2006
      • 82

      #3
      Originally posted by Lazareth
      Hi,

      I am new to VB. I have just written a simple program to help me calculate GST.

      Everything works good but I was wanting to adjust my program so that as well as clicking on the buttons I could also use keyboard keys programmed to each button.

      Does anyone know a simple solution to accomplish this?
      hi hariharan,
      how can we make the keyboard keys programmed to each control? and how will the key up event recognize that? cd you tell me abt that?

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by aaryan
        hi hariharan,
        how can we make the keyboard keys programmed to each control? and how will the key up event recognize that? cd you tell me abt that?

        Whenever your form Got focus then each and every input by keyboard or mouse will follow by the control events of every control

        The code sample.


        Private Sub Form_Load()
        Me.KeyPreview = True
        End sub


        Private Sub Form_KeyDown(Ke yCode As Integer, Shift As Integer)
        If KeyCode = vbKeyEscape Then Unload Me
        End Sub

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by hariharanmca
          Whenever your form Got focus then each and every input by keyboard or mouse will follow by the control events of every control

          The code sample.


          Private Sub Form_Load()
          Me.KeyPreview = True
          End sub


          Private Sub Form_KeyDown(Ke yCode As Integer, Shift As Integer)
          If KeyCode = vbKeyEscape Then Unload Me
          End Sub


          if there is no any statement in the event then it won't consider

          Comment

          • Lazareth
            New Member
            • Jan 2007
            • 29

            #6
            hi,

            cheers for the reply.

            do I need to use this code for every button.

            where in the code do I stipulate which button needs to be pushed

            I have 14 buttons... 0-9 , C , . , Add GST , Subtract GST

            Comment

            • hariharanmca
              Top Contributor
              • Dec 2006
              • 1977

              #7
              Originally posted by Lazareth
              hi,

              cheers for the reply.

              do I need to use this code for every button.

              where in the code do I stipulate which button needs to be pushed

              I have 14 buttons... 0-9 , C , . , Add GST , Subtract GST

              no, already you writen the buttons events in ButtonName_Clic k() EVENT
              so just call that method in form KeyUp or KeyDown Event( if you know the Keyascii then you can use in KeyPress Event)

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by hariharanmca
                no, already you writen the buttons events in ButtonName_Clic k() EVENT
                so just call that method in form KeyUp or KeyDown Event( if you know the Keyascii then you can use in KeyPress Event)
                You should not invoke the button's click event directly. The accepted method to click a button from code is to execute Button = True, I believe. So you could do something like:
                Code:
                Private Sub Form_KeyPress(KeyAscii As Integer)
                  Select Case KeyAscii
                    Case vbKey1
                      Button1 = true
                    Case vbKey2
                      Button2 = true
                    Case vbKeyEnter
                      ButtonEquals = True
                ' And so on...
                  End Select
                End Sub

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  Originally posted by Killer42
                  You should not invoke the button's click event directly. The accepted method to click a button from code is to execute Button = True, I believe. So you could do something like:
                  Code:
                  Private Sub Form_KeyPress(KeyAscii As Integer)
                    Select Case KeyAscii
                      Case vbKey1
                        Button1 = true
                      Case vbKey2
                        Button2 = true
                      Case vbKeyEnter
                        ButtonEquals = True
                  ' And so on...
                    End Select
                  End Sub

                  Private Sub Form_KeyPress(K eyAscii As Integer)
                  Select Case KeyAscii
                  Case vbKey1
                  Button1_Click
                  Case vbKey2
                  Button2_Click
                  Case vbKeyEnter
                  ButtonEquals_Cl ick
                  ' And so on...
                  End Select
                  End Sub

                  i cannot find any diff between this and the above coding

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    Originally posted by Lazareth
                    hi,

                    cheers for the reply.

                    do I need to use this code for every button.

                    where in the code do I stipulate which button needs to be pushed

                    I have 14 buttons... 0-9 , C , . , Add GST , Subtract GST
                    You could just type the ampersand '&' in front of the keyboard letter / number in the button caption that you want to press and hold down the Alt key while you press them. Or maybe I am just a little simple minded...

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by hariharanmca
                      Private Sub Form_KeyPress(K eyAscii As Integer)
                      Select Case KeyAscii
                      Case vbKey1
                      Button1_Click
                      ...
                      End Select
                      End Sub
                      i cannot find any diff between this and the above coding
                      The difference is that this code directly calls the Button1_Click routine, while my version sets the Value of the button to True, which triggers the click event. In most cases the outcome will be the same. However, the direct call has traditionally been discouraged. I don't recall exactly why - does anyone else here remember?

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by willakawill
                        You could just type the ampersand '&' in front of the keyboard letter / number in the button caption that you want to press and hold down the Alt key while you press them. Or maybe I am just a little simple minded...
                        Good point will, and I wonder whether it was considered. However, it might not be reasonable to expect the user to use a calculator-type program while holding down the Alt key. :)

                        Comment

                        • willakawill
                          Top Contributor
                          • Oct 2006
                          • 1646

                          #13
                          Originally posted by Killer42
                          Good point will, and I wonder whether it was considered. However, it might not be reasonable to expect the user to use a calculator-type program while holding down the Alt key. :)
                          It might not and the original question asked for a simple method.

                          Comment

                          Working...