Results 1 to 15 of 15

Thread: Two KeyCodes

  1. #1

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    How do I use two KeyCodes at the same time ?
    For example, when the user is pressing F1 (KeyCode: &H70)
    and F2 (KeyCode: &H71) a messagebox or something else
    should appear. Note: this message-box should appear
    when the user presses this two buttons at the same time!!!
    If he only presses one button nothing should happen !!!!! :d


    Thanks for some adive, Matt-D(eutschland)

    [Edited by Matt-D on 05-02-2000 at 09:36 AM]

    [Edited by Matt-D on 05-02-2000 at 12:23 PM]

  2. #2
    Guest
    Hope this is what you need

    Private Sub Form_KeyDown(KeyCode As Integer, _
    Shift As Integer)
    If KeyCode = 112 Then
    MsgBox "F1"
    ElseIf KeyCode = 113 Then
    MsgBox "F2"
    End If

  3. #3
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    You can also use Select Case:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, _
    Shift As Integer)
    
    Select Case KeyCode
        Case 112
            MsgBox "F1"
        Case 113
            MsgBox "F2"
    End Select
    
    End Sub

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Well, to check if both the keys are down, you can use GetKeyState api. Bit 15 of the returnvalue is 1 if the key is down, otherwise it is 0 (of course).
    Code:
    Option Explicit
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Private Const VK_F1 = &H70
    Private Const VK_F2 = &H71
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim retVal As Long
        If KeyCode = vbKeyF1 Then
            'check if F2 is also down
            retVal = GetKeyState(VK_F2)
        ElseIf KeyCode = vbKeyF2 Then
            'check if F1 is also down
            retVal = GetKeyState(VK_F1)
        End If
        If (retVal And &H4000) = &H4000 Then
            MsgBox "Now"
        End If
    End Sub
    I hope I used the correct hex value, but a quick test worked.

    [Edited by Frans C on 05-02-2000 at 02:03 PM]

  5. #5

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    Isn't there a way without using the API?
    For this programm I use VB 3 and it doesn't work with it!

    There must be a way to do it ....................................
    Matt-D(eutschland)

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I don't know of another way, but it should work in VB3 as well. Or if you are using 16 bit windows version you can try to change "user32" in "user", but I am not sure about that.

  7. #7
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Here is a way to do it without an API call. You might want to add a check for time interval between the KeyDown events because as this code stands, you can press the F1 and then wait as long as you want and then Press the F2 and trigger the 'Proper Key Combo Detected code.

    I'd really suggest trying to get the API call working, but if you can't this might be an alternative you can use.

    Also, I'd really recommend not using the F1 key in the combination since F1 is typically used to invoke help.

    Hope this helps.

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Static multiKey As Integer
        
        multiKey = multiKey + KeyCode
        
        Select Case multiKey
            Case 112 'F1
                'Detect Duplicate F1 press
                If multiKey = 224 Then
                    multiKey = 0
                End If
            Case 113 'F2
                'Detect Duplicate F2 press
                If multiKey = 226 Then
                    multiKey = 0
                End If
            Case 225 'F1 + F2
                'Proper Key Combo Detected
                'Your Code Here
                MsgBox "F1 & F2 Pressed"
                multiKey = 0
            Case Else
                multiKey = 0
        End Select
                
    End Sub
    [Edited by jcouture100 on 05-02-2000 at 02:41 PM]
    JC

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    it is a variable that keeps the data inside it, until it is changed even if the sub is not running

  9. #9

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    Thanks







  10. #10
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    what is it for?

  11. #11

    Wink

    jcouture100 has given a good solution
    but in the code whate he suplyed
    i dont know why he is checking for multykey=224

    if multykey=112 then only the control of execution comes inside that block.
    so there no need to have those if conditions.

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    The api one for multiple keys is independent from any keydown events in your application, so this is good for gamedevelopers:
    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Public Sub DoKeys()
        If GetAsyncKeyState(vbKeyF1) and GetAsyncKeyState(vbKeyF2) Then blablabla then msgbox "HEhehe!"
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    what is it for ?

  14. #14

    Thread Starter
    Hyperactive Member Matt-D's Avatar
    Join Date
    Nov 1999
    Location
    Mettmann, Germany
    Posts
    305
    It's just for fun.
    With this extra feature the user chnages the backcolor
    and the font of a grid.

    [i] Matt-D(eutschland)

  15. #15
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    like a data grid ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width