Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selecting Text (Office 2003)

    I have an application with a comment (text) box. The users write comments about a customer they are evaluating. They work primarily on laptops.

    Due to lots of problems with accidentally highlighting and deleting text by simultaneously hitting the right Enter key and the various arrow keys, I have used the KeyDown event to capture those combinations and turn them off. For the most part, the users are happy as they don't have to rekey what they accidentally erased.

    Problem is that sometimes they WANT to highlight text, to either copy or move it. They can do that with the mouse. Some would like to use it with the keyboard.

    I attempted to turn the keys back on by including the control key in the mix, like shown below:

    If vbKeyControl > 0 And vbKeyShift > 0 Then
    Select Case KeyCode
    Case vbKeyLeft
    KeyCode = 1
    Case vbKeyRight
    KeyCode = 1
    Case vbKeyUp
    KeyCode = 1
    Case vbKeyDown
    KeyCode = 1
    End Select
    End If

    I think at least part of the problem is that the even fires as soon as I hit the Ctrl key, and pressing the Shift then has no effect. Is there a way to get the code to look for the key combination?

    I appreciate any thoughts or suggestions on how to do it differently.

    George

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Selecting Text (Office 2003)

    vbKeyControl and vbKeyShift are constants, they are not variables reflecting the state of the corresponding keys.
    The KeyDown and KeyUp event procedures have an argument Shift that indicates which of the modifier keys was down. There are constants acShiftMask, acAltMask and acCtrlMask that can be used in conjunction with Shift to test:

    If Shift And acCtrlMask Then
    ' Control key down
    ...
    Else
    ' Control key not down
    ...
    End If

  3. #3
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting Text (Office 2003)

    I tried it that way, too, as shown below.

    If (Shift And acShiftMask) > 0 And (Shift And acCtrlMask) > 0 Then
    Select Case KeyCode
    Case vbKeyLeft
    KeyCode = 1
    Case vbKeyRight
    KeyCode = 1
    Case vbKeyUp
    KeyCode = 1
    Case vbKeyDown
    KeyCode = 1
    End Select
    End If

    This doesn't work, either. It captures the shift key and doesn't seem to caputure the control key.

    Thanks!

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Selecting Text (Office 2003)

    I'm sorry, I don't understand that. When I test similar code on my PC, it tests whether Shift AND Ctrl are down.

    By the way, what is the purpose of setting KeyCode to 1?

  5. #5
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting Text (Office 2003)

    I thought, obviously incorrectly, that since I used KeyCode = 0 to turn off Shift + the arrow keys earlier in the procedure, I needed to set it to 1 to turn it back on. If I remove the KeyCode = 1 line, and press Shift + Ctrl, the arrow keys still don't work.

    I'm probably going about this all wrong. Maybe I can make it clearer. First thing in the KeyDown event procedure, I look to see if the Shift key and one of the arrows are pressed together. If they are, they are turned off to prevent accidental selecting of text.

    Now the users want a key combination to use to select text on purpose. In essence to override the override. Is there some other key combination I could use, perhaps in conjunction with a macro/module with some kind of language to reset the Shift + arrow key combination to normal behavior? For example, Ctrl+Shift+H could run a macro that calls a procedure to allow the arrow keys to perform normally.

    I'm at a loss, obviously.

    Thanks for your patience.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Selecting Text (Office 2003)

    You can use a module-level variable to toggle the behavior of the arrow keys. Near the top of the form module, below lines starting with Option but before all Subs and Functions, insert a line

    Dim blnArrows As Boolean

    Change the KeyDown event procedure as follows (substitute the correct name)

    Private Sub txtComment_KeyDown(KeyCode As Integer, Shift As Integer)
    If (Shift And acCtrlMask) > 0 And (Shift And acShiftMask) > 0 And _
    KeyCode = vbKeyH Then
    blnArrows = Not blnArrows
    KeyCode = 0
    ElseIf blnArrows = False Then
    Select Case KeyCode
    Case vbKeyLeft, vbKeyUp, vbKeyRight, vbKeyDown
    KeyCode = 0
    End Select
    End If
    End Sub

    You can add extra checks, of course.

  7. #7
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selecting Text (Office 2003)

    Thanks so much! I'll try it.

    And, thanks for the hint on the Case statements. I didn't realize they could be combined on one line.

Posting Permissions

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