locked
KeyUp and KeyDown problem RRS feed

  • Question

  • Hi all,

     I am implementing an input handling system for an application using C# and .NET. I encountered a problem with KeyUp and KeyDown events of a Control, namely the KeyDown event of a key terminates that of another. Eg. if the user holds down Alt, that keep generating Alt key down event until the user hits another key say X. After the key down event of X is fired, Control no longer fires KeyDown event of Alt even if it is still held down. Is there any way to get around this? Hope someone can advice on this.

    Thuan
    Thursday, September 20, 2007 5:44 AM

Answers

All replies

  • Got a MenuStrip in your form?  Alt will take the focus off your control, even though it looks like it still has it.  Moved to Windows Forms General.
    Thursday, September 20, 2007 6:58 AM
  • When you create an application that accepts the user's keystrokes, you may also want to monitor for modifier keys such as the SHIFT, ALT, and CTRL keys. When a modifier key is pressed in combination with other keys, or with mouse clicks, your application can respond appropriately. For example, if the letter S is pressed, this may simply cause an "s" to appear on the screen, but if the keys CTRL+S are pressed, the current document may be saved. If you handle the KeyDown event, the Modifiers property of the KeyEventArgs received by the event handler specifies which modifier keys are pressed. Alternatively, the KeyData property of KeyEventArgs specifies the character that was pressed as well as any modifier keys combined with a bitwise OR. However, if you are handling the KeyPress event or a mouse event, the event handler does not receive this information. In this case, you must use the ModifierKeys property of the Control class. In either case, you must perform a bitwise AND of the appropriate Keys value and the value you are testing. The Keys enumeration offers variations of each modifier key, so it is important that you perform the bitwise AND with the correct value. For example, the SHIFT key is represented by Shift, ShiftKey, RShiftKey and LShiftKey The correct value to test SHIFT as a modifier key is Shift. Similarly, to test for CTLR and ALT as modifiers you should use the Control and Alt values, respectively.

    <http://msdn2.microsoft.com/en-us/library/yazd4ct6.aspx>
    Thursday, September 20, 2007 8:01 AM
  • Maybe I didn't specific enough information here, what I am trying to achieve is as below:

    Scenario 1:
    Ctrl + somekey (e.g. A) does one action.
    Ctrl + Mouse Left button does another action.

    I want the user to be able to perform the two different actions without releasing the Ctrl key. That's one issue, which can be resolved if I can find out if any modifier keys are active on mouse down.

    Scenario 2:
    If I hold down any key (i.e. can be alt, ctrl or shift or other character key for that matter), and hit another different key, (e.g. holding down the arrow key and 'A'), when the later key is released (i.e. 'A') I still want to be able to know that the arrow key is held down or released. How do I do that? Tracing the control key down event shows something like:

    1. KeyDown: Up <---- Up arrow is held down
    2. KeyDown: Up
    3. KeyDown: Up
    4. KeyDown: Up
    5. KeyDown: A  <---- Up arrow and A are both held down
    6. KeyDown: A
    7. KeyDown: A
    8. KeyUp: A  <--- Released A but still holding down Up arrow

    Why is it after event 8, Control no longers fires KeyDown for Up arrow even if it is held down? Even if I release Up arrow key after event 8 I don't get a KeyUp at all. Am I missing something here?




    Thursday, September 20, 2007 9:15 AM
  • #1: check this thread.
    #2: check this thread.
    Thursday, September 20, 2007 11:12 AM