Using Modifier Keys with Command Buttons


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Using Modifier Keys with Command Buttons

  1. #1
    Join Date
    Feb 2010
    Location
    Central WV, USA
    Posts
    212

    Using Modifier Keys with Command Buttons

    In Photoshop there are several dialogs that allow you to change the function of a command button by using a modifier key(Shift/Alt/Ctr). If the command button has focus and you hold down the alt key for example the cmd text changes and the button performes a different function. I'm wanting to duplicate this functionality but i'm not sure how to make it work. It would have to be set up so that not all keystrokes are captured. I think it's called keypreview. And I think I would then need to use the system event args (or maybe sender) to ID the keypress then handle the event based on that keypress. Once I figure out the capture code I'm assuming I could then use an 'IF' statement to execute the associated code.

  2. #2
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    There is a PreviewKeyDown event on the Button class. It is triggered whenever the user activates a key while the Button ALREADY has the focus.

    This event provides a standard e parameter that has 3 boolean properties indicating if the Control, Alt and/or Shift key is/are pressed.

    Note that the buttons has to ALREADY have the focus (which is what you stated in your post). The user needs to first click on the button or move to it with the keyboard, and then press the desired key.

    It cannot be used to detect a Ctrl-Click combination for instance. This would need to be handled in another way.

    -----

    The KeyPreview that was lingering somewhere in you mind does indeed exists, but it has another purpose.

    Normally, as soon as there is at least one control that can receive the focus on a form, the form itself never receives any of the 3 Key... events. Only the control that has the focus receives the KeyDown, KeyUp and KeyPress events

    There are some situations in which you might want to trap a KeyPress no matter which control has the cursor. Say for instance that you would let the user perform an operation from anywhere in the form when he activates Ctrl-2. This can be set easily if there is a menu on the form, but if there is not menu, you are out of luck... unless you set the KeyPreview property of the form to True.

    When KeyPreview is set, all the keys activated while on the form will trigger the Form.Key... events before doing so on the Control.Key... events. You could thus Catch your Ctrl-2 from anywhere, as long as the form is activated.

    A lot of stuff will pass throught the Form.Key... events however, and I have known very fast typists complaining that this made the keyboard sluggish, specially if you needed to intercept many different keys (big Select...Case). But that was a few years ago, and I suspect that with modern fast computers, this would not be a concern anymore.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  3. #3
    Join Date
    Feb 2010
    Location
    Central WV, USA
    Posts
    212
    Is this what you were talking about

    Code:
    Private Sub cmdApplyPayment_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles cmdApplyPayment.PreviewKeyDown
            'Cmd ApplyPayment KeyPrevDown
            'Swith to different command button Mode
            Static ButtonMode As Boolean = False
            If e.Alt Then
                ButtonMode = True
                cmdApplyPayment.Text = "Reset Collections"
            Else
                ButtonMode = False
                cmdApplyPayment.Text = "&Add Payment to OpenRecs"
            End If
        End Sub
    Then in my cmd_click I would then handle the execution based on the booelan value (ButtonMode)

    My code will change it once, but will not change it back. The photoshop example actually only changes it while the alt key is being held down, then once released it returns to normal.
    Last edited by rrjii2000; 11-23-2010 at 08:16 PM.

  4. #4
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    This is what I was talking about, but with a little problem. And then, after you explain a little more what you want it to do, I see that it is not the right solution. You did not mentioned holding down the Alt button on the first post.

    -----

    The "little problem" is that because you declare the ButtonMode variable in the PreviewKeydown event, it is a local variable that is visible only in that event. The Click won't have access to its value.

    If you want to be able to use a variable in 2 different events, you need to declare it as a form level variable, in the declaration section between the beginning of the form and the first procedure. It will then be available from everywhere in the form. And you don't have to declare it Static, the standard Dim will do, because it will exist as long as the form is loaded in memory.

    -----

    From your additional information, we learn that you also need to detect the release of the Alt key. The PreviewDown event does not catch that, it is only triggered when you press down on the key.

    We thus need to switch to another event, actually, a pair of events. You can also detect the activation of the Control, Alt and Shift keys in the KeyUp and KeyDown events. If I understand your purpose correctly, you would then set your ButtonMode to True on the KeyDown when e.Alt is True, and set if to False on the KeyUp if e.Alt is False. Then the Click would be able to know if Alt is still activated.

    But once again, the cursor needs to already be on the button before you activate the key.

    If you want to be able press Alt from anywhere on the form, and while you are holding it go to presss the button, you will need to trap the KeyDown and KeyUp on the form instead of doing it on the Button. An in order to do so, you will have to set the form KeyPreview property to True as explained in my previous post.
    Last edited by JBourgeois; 11-23-2010 at 10:40 PM.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  5. #5
    Join Date
    Feb 2010
    Location
    Central WV, USA
    Posts
    212
    I would have found the problem with my static varable when I went to write my click routine, don't know what I was thinking. But I have it working now...

    Code:
    Private Sub cmdApplyPayment_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmdApplyPayment.KeyDown
            'cmd Apply Payment - Key Down
    
            'Apply the button mode
            If e.Alt Then
                ButtonMode = True
                cmdApplyPayment.Text = "Reset Collections"
            Else
                ButtonMode = False
                cmdApplyPayment.Text = "&Apply Payment"
            End If
            
        End Sub
    
        Private Sub cmdApplyPayment_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmdApplyPayment.KeyUp
            'cmd Apply Payment - Key Up
    
            'Release the Button Mode
            ButtonMode = False
            cmdApplyPayment.Text = "&Apply Payment"
        End Sub
    
    Private Sub cmdApplyPayment_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmdApplyPayment.click
            'cmd Apply Payment - Click
    
            If ButtonMode Then
                ' Do Condition 1
    
            Else
                ' Do Condition 2
    
            End If
            
        End Sub
    It's a little laggy and doesn't always switch right away, but i'm on an older XP machine so I guess that is to be expected. And it works reguardless of the keypreview property.
    Last edited by rrjii2000; 11-24-2010 at 10:56 AM.

Similar Threads

  1. Setting up HOT keys
    By rrjii2000 in forum .NET
    Replies: 4
    Last Post: 03-19-2010, 01:48 AM
  2. get the array of keycodes for the keys that were pressed
    By Michael \(michka\) Kaplan in forum VB Classic
    Replies: 8
    Last Post: 06-07-2002, 09:23 PM
  3. Replies: 0
    Last Post: 07-18-2001, 05:11 PM
  4. Replies: 0
    Last Post: 07-18-2001, 05:11 PM
  5. Primary keys & Triggers???
    By Beginner-Brad in forum Database
    Replies: 1
    Last Post: 01-24-2001, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center