KeyCode combination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #1

    KeyCode combination

    I'm venturing (for the first time) into the realm of the KeyDown, KeyPress, KeyUp, etc. domain. I have successfully managed to get it to where it will recognize when I have pressed Ctrl + 1, Ctrl + 2, etc. I just want to make sure that I'm doing it in the way that is best. I had so much trouble figuring out how to do the multi-key testing that I'm just not sure. Here is my code:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim ShiftKey As Integer
    Dim strCharacter As String
    
    strCharacter = Chr(KeyCode)
    
    ShiftKey = Shift And 7
    
    If ShiftKey = 3 Then
        Select Case strCharacter
            Case 1 To 9
                Call insertBlank(" %Blank" & strCharacter & "% ")
                
            Case "a"
                Call insertBlank(" %Accused% ")
            
            Case "o"
                Call insertBlank(" %Officer% ")
                
            Case "p"
                Call insertBlank(" %PenalCode% ")
                
        End Select
    End If
    
    End Sub
    Code:
    Private Sub insertBlank(strBlank As String)
    Dim strWording As String
    
    With Me.LegalWording
        .SetFocus
        strWording = .Text
        strWording = strWording & strBlank
        .Value = strWording
        .SelStart = Len(.Text)
    End With
    
    End Sub
    I'm only testing for the shift of 3 (the VbCtrlMask + VbShiftMask) as I only need to know if both the Ctrl and Shift keys are pressed at the same time with an additional number or letter. I then am testing for the third key that was pressed (strCharacter). I then test for that value in the Select Case statement to tack onto the end of the text in LegalWording (a textbox on the form).

    Again, this does work (surprisingly so since I did it on my own), but I just want to know if this is an acceptable way.
    Last edited by Seth Schrock; Dec 27 '12, 02:26 AM. Reason: fixed duplicate code posting.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Checking the Shift value can be done as you have, by using the value 7 and storing it in a variable, but it could also be done as a single comparison :
    Code:
    If (Shift And (acShiftMask Or acCtrlMask)) = (acShiftMask Or acCtrlMask) Then
    I generally recommend using predefined constants where available. It makes the code more easily read and understood.

    In the same way, there is little need to store strCharacter, when you can use KeyCode instead, and compare with vbKey1, vbKey9, vbKeyA, vbKeyO and vbKeyP.

    Otherwise, I would expect your code to work. It has no logic flaws in it. It looks pretty good TBF - especially for a first stab at it :-)

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      So if I'm understanding correctly, your line of code replaces line 9 and makes line 7 not needed (also removes any dim statement not used). Also, my Select case would be not testing strCharacter, but would be testing for KeyCode while each case would be VbKey1, VbKey2, etc.

      Can I do a select case on VbKey1 to VbKey9 as I am for strCharacter or would I have to list each on individually?

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        Alright, I got brave and tried what I thought you meant out and of course, it worked. Thanks NeoPa. I'm starting to get things figured out and am getting braver with trying code :)

        So here is what I have now (with the rest of the keycodes that I plan on using):
        Code:
        Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        
        'Tests if Shift + Ctrl were pressed simultaneously
        If (Shift And (acShiftMask Or acCtrlMask)) = (acShiftMask Or acCtrlMask) Then
            
            'Tests other key pressed
            Select Case KeyCode
                Case vbKey1 To vbKey9 'For Blanks: number key pressed will be added to the end of "Blank"
                    Call insertBlank(" %Blank" & Chr(KeyCode) & "% ")
                    
                Case vbKeyA
                    Call insertBlank(" %Accused% ")
        
                Case vbKeyO
                    Call insertBlank(" %Officer% ")
        
                Case vbKeyP
                    Call insertBlank(" %PenalCode% ")
        
                Case vbKeyD
                    Call insertBlank(" %day% ")
        
                Case vbKeyM
                    Call insertBlank(" %month% ")
        
                Case vbKeyY
                    Call insertBlank(" %year% ")
                    
            End Select
        End If
        
        End Sub

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32669

          #5
          Originally posted by Seth
          Seth:
          Can I do a select case on VbKey1 to VbKey9 as I am for strCharacter or would I have to list each on individually?
          It seems you already know the answer is Yes Seth :-) Good for you.

          You could also set up a constant to use for checking Ctrl and Shift :
          Code:
          Private Const conCtrlShiftMask As Long = acShiftMask Or acCtrlMask
          
          Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
              If (Shift And conCtrlShiftMask) = conCtrlShiftMask Then

          Comment

          Working...