Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. multiple keypresses

multiple keypresses

Scheduled Pinned Locked Moved Visual Basic
helpquestion
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheMrProgrammer
    wrote on last edited by
    #1

    hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??

    TheMrProgrammer

    H N 2 Replies Last reply
    0
    • T TheMrProgrammer

      hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??

      TheMrProgrammer

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      The only way I know to do this is to use the API call GetKeyboardState(), unless someone knows of a simpler way.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      1 Reply Last reply
      0
      • T TheMrProgrammer

        hello everyone in my app i need to detect more than one keypresses like while key "1" is pressed i need to know if key "2" is also pressed ,ie, both keys are pressed at the same time or not? can any one help me??

        TheMrProgrammer

        N Offline
        N Offline
        nlarson11
        wrote on last edited by
        #3

        write your own code using the key_down/key_up events - the below code writes out to the debug window each key has been pressed(down). once you lift up on a key it shows it has been released(up).

        Private _KeyDown As New List(Of Int16)

        Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            If Not \_KeyDown.Contains(e.KeyCode) Then
                \_KeyDown.Add(e.KeyCode)
                Debug.Print("d:" & Chr(e.KeyValue))
            End If
        End Sub
        
        Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            If \_KeyDown.Contains(e.KeyCode) Then
                \_KeyDown.Remove(e.KeyCode)
                Debug.Print("u:" & Chr(e.KeyValue))
            End If
        End Sub
        

        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

        J 1 Reply Last reply
        0
        • N nlarson11

          write your own code using the key_down/key_up events - the below code writes out to the debug window each key has been pressed(down). once you lift up on a key it shows it has been released(up).

          Private _KeyDown As New List(Of Int16)

          Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
              If Not \_KeyDown.Contains(e.KeyCode) Then
                  \_KeyDown.Add(e.KeyCode)
                  Debug.Print("d:" & Chr(e.KeyValue))
              End If
          End Sub
          
          Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
              If \_KeyDown.Contains(e.KeyCode) Then
                  \_KeyDown.Remove(e.KeyCode)
                  Debug.Print("u:" & Chr(e.KeyValue))
              End If
          End Sub
          

          'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

          J Offline
          J Offline
          JR212
          wrote on last edited by
          #4

          Nice code. I can use this to, but a question: It seem to me that the maximum numbre of simultanious keys is 3. Is this normal?

          D D 2 Replies Last reply
          0
          • J JR212

            Nice code. I can use this to, but a question: It seem to me that the maximum numbre of simultanious keys is 3. Is this normal?

            D Offline
            D Offline
            Dalek Dave
            wrote on last edited by
            #5

            Historical. Ctrl Alt Delete Ctrl Shift (a Key) Shift AltGr (a Key) Any more than three is deemed to be elbows on the keyboard. I believe this can be altered, but alas not by me. Dave Kreskowiak may know this. Ask him,

            ------------------------------------ "Possessions make you poor, wealth is measurable only in experience." Sun Tzu 621BC

            J 1 Reply Last reply
            0
            • D Dalek Dave

              Historical. Ctrl Alt Delete Ctrl Shift (a Key) Shift AltGr (a Key) Any more than three is deemed to be elbows on the keyboard. I believe this can be altered, but alas not by me. Dave Kreskowiak may know this. Ask him,

              ------------------------------------ "Possessions make you poor, wealth is measurable only in experience." Sun Tzu 621BC

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              I adapted Larson's code just slightly (you need to disable TextBox1 by the way):

              Private WithEvents \_KeyDown As New List(Of Int16)
              
              Private Sub TextBox1\_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
                  If Not \_KeyDown.Contains(e.KeyCode) Then
                      \_KeyDown.Add(e.KeyCode)
                      TextBox1.Text &= Chr(e.KeyCode)
                  End If
              End Sub
              
              Private Sub TextBox1\_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
                  If \_KeyDown.Contains(e.KeyCode) Then
                      \_KeyDown.Remove(e.KeyCode)
                      TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.IndexOf(Chr(e.KeyCode)), 1)
                  End If
              End Sub
              

              I managed to press more than 2 keys, but not all combos work, and some combos act like alt+ctrl etc. and other combos somehow work without a hitch.

              My advice is free, and you may get what you paid for.

              1 Reply Last reply
              0
              • J JR212

                Nice code. I can use this to, but a question: It seem to me that the maximum numbre of simultanious keys is 3. Is this normal?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                The is the normal max that will work reliably. If you want more than that, you're going to have to go to DirectX and it's DirectInput library, or use the XNA Framework (C#) to simplify using all the DirectX stuff.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                J 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  The is the normal max that will work reliably. If you want more than that, you're going to have to go to DirectX and it's DirectInput library, or use the XNA Framework (C#) to simplify using all the DirectX stuff.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  J Offline
                  J Offline
                  JR212
                  wrote on last edited by
                  #8

                  thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups