reacting to keyup?!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

reacting to keyup?!!

Joe2003Joe2003 Posts: 92Member
Hey

I have a pannel, all I want to do is if the user presses the up key I want a messagebox to appear that says "UP", if they press down a messagebox to appear that says "DOWN" and so on for left and right.

is this possible, I have been trying to use the keyup event handler but I cant get it to work.

cheers

Joel

Comments

  • DrMartenDrMarten Posts: 748Member
    .NET Framework Class Library

    Control.KeyPress EventSee Also
    Control Class | Control Members | System.Windows.Forms Namespace | OnKeyPress | Control Members (Visual J# Syntax) | Managed Extensions for C++ Programming
    Requirements
    Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
    Language
    C#

    C++

    JScript

    Visual Basic

    Show All
    Occurs when a key is pressed while the control has focus.

    [Visual Basic]
    Public Event KeyPress As KeyPressEventHandler

    [C#]
    public event KeyPressEventHandler KeyPress;

    [C++]
    public: __event KeyPressEventHandler* KeyPress;

    [JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

    Event Data
    The event handler receives an argument of type KeyPressEventArgs containing data related to this event. The following KeyPressEventArgs properties provide information specific to this event.

    Property Description
    Handled Gets or sets a value indicating whether the KeyPress event was handled.
    KeyChar Gets the character corresponding to the key pressed.

    Remarks
    Key events occur in the following order:

    KeyDown
    KeyPress
    KeyUp
    The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

    To handle keyboard events only at the form level and not allow other controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true.

    For more information about handling events, see Consuming Events.

    .NET Compact Framework Platform Note: In addtion to the base Control class, the .NET Compact Framework provides support for this event on controls with Service Pack 2 and later. The Smart Devices Developer Community Web site provides information about the availability of service packs, see http://go.microsoft.com/fwlink/?LinkId=16561.

    Example
    [Visual Basic]
    ' Boolean flag used to determine when a character other than a number is entered.
    Private nonNumberEntered As Boolean = False


    ' Handle the KeyDown event to determine the type of character entered into the control.
    Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
    Handles textBox1.KeyDown
    ' Initialize the flag to false.
    nonNumberEntered = False

    ' Determine whether the keystroke is a number from the top of the keyboard.
    If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
    ' Determine whether the keystroke is a number from the keypad.
    If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
    ' Determine whether the keystroke is a backspace.
    If e.KeyCode <> Keys.Back Then
    ' A non-numerical keystroke was pressed.
    ' Set the flag to true and evaluate in KeyPress event.
    nonNumberEntered = True
    End If
    End If
    End If
    End Sub 'textBox1_KeyDown


    ' This event occurs after the KeyDown event and can be used
    ' to prevent characters from entering the control.
    Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
    Handles textBox1.KeyPress
    ' Check for the flag being set in the KeyDown event.
    If nonNumberEntered = True Then
    ' Stop the character from being entered into the control since it is non-numerical.
    e.Handled = True
    End If
    End Sub 'textBox1_KeyPress
    End Class 'Form1

    [C#]
    // Boolean flag used to determine when a character other than a number is entered.
    private bool nonNumberEntered = false;

    // Handle the KeyDown event to determine the type of character entered into the control.
    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
    // Determine whether the keystroke is a number from the keypad.
    if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
    {
    // Determine whether the keystroke is a backspace.
    if(e.KeyCode != Keys.Back)
    {
    // A non-numerical keystroke was pressed.
    // Set the flag to true and evaluate in KeyPress event.
    nonNumberEntered = true;
    }
    }
    }
    }

    // This event occurs after the KeyDown event and can be used to prevent
    // characters from entering the control.
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
    // Stop the character from being entered into the control since it is non-numerical.
    e.Handled = true;
    }
    }

    [C++]
    // Boolean flag used to determine when a character other than a number is entered.
    private:
    bool nonNumberEntered;

    // Handle the KeyDown event to determine the type of character entered into the control.
    void textBox1_KeyDown(Object* /*sender*/, System::Windows::Forms::KeyEventArgs* e)
    {
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9)
    {
    // Determine whether the keystroke is a number from the keypad.
    if (e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9)
    {
    // Determine whether the keystroke is a backspace.
    if(e->KeyCode != Keys::Back)
    {
    // A non-numerical keystroke was pressed.
    // Set the flag to true and evaluate in KeyPress event.
    nonNumberEntered = true;
    }
    }
    }
    }

    // This event occurs after the KeyDown event and can be used to prevent
    // characters from entering the control.
    void textBox1_KeyPress(Object* /*sender*/, System::Windows::Forms::KeyPressEventArgs* e)
    {
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
    // Stop the character from being entered into the control since it is non-numerical.
    e->Handled = true;
    }
    }

    [JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.

    Requirements
    Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

    See Also
    Control Class | Control Members | System.Windows.Forms Namespace | OnKeyPress | Control Members (Visual J# Syntax) | Managed Extensions for C++ Programming


    Syntax based on .NET Framework version 1.1.
    Documentation version 1.1.1.



    Regards,

    Dr M.



  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-3-11 7:49:9[/red][/b][hr]
    Hi,

    In plain ENGLISH if you want keypress or keydown events handled wherever you are in a form then set this eg. FORM1 property to TRUE otherwise the default is to have the control with FOCUS ( pointed to ) hadle the event.

    Regards,

    Dr M.


    Show All
    Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

    [Visual Basic]
    Public Property KeyPreview As Boolean

    [C#]
    public bool KeyPreview {get; set;}

    [C++]
    public: __property bool get_KeyPreview();
    public: __property void set_KeyPreview(bool);

    [JScript]
    public function get KeyPreview() : Boolean;
    public function set KeyPreview(Boolean);

    Property Value
    true if the form will receive all key events; false if the currently selected control on the form receives key events. The default is false.

    Remarks
    When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event-handling methods of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event-handling method to true.

    You can use this property to process all keystrokes in your application and either handle the keystroke or call the appropriate control to handle the keystroke. For example, when an application uses function keys, you might want to process the keystrokes at the form level rather than writing code for each control that might receive keystroke events.

    Note If a form has no visible or enabled controls, it automatically receives all keyboard events.
    Example
    [Visual Basic, C#] The following code example demonstrates setting a form's KeyPreview property to true and handlingthe key events at the form level. To run the example, paste the following code in a blank form.

    [Visual Basic]
    Imports System.Windows.Forms

    Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Declare the controls contained on the form.
    Private WithEvents button1 As MyMnemonicButton
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    Public Sub New()
    MyBase.New()

    ' Set KeyPreview object to true to allow the form to process
    ' the key before the control with focus processes it.
    Me.KeyPreview = True

    ' Add a MyMnemonicButton.
    button1 = New MyMnemonicButton
    button1.Text = "&Click"
    button1.Location = New System.Drawing.Point(100, 120)
    Me.Controls.Add(button1)

    ' Initialize a ListBox control and the form itself.
    Me.ListBox1 = New System.Windows.Forms.ListBox
    Me.SuspendLayout()
    Me.ListBox1.Location = New System.Drawing.Point(8, 8)
    Me.ListBox1.Name = "ListBox1"
    Me.ListBox1.Size = New System.Drawing.Size(120, 95)
    Me.ListBox1.TabIndex = 0
    Me.ListBox1.Text = "Press a key"
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.ListBox1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    ' The form will handle all key events before the control with
    ' focus handles them. Show the keys pressed by adding the
    ' KeyCode object to ListBox1. Ensure the processing is passed
    ' to the control with focus by setting the KeyEventArg.Handled
    ' property to false.
    Private Sub Form1_KeyDown(ByVal sender As Object, _
    ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    ListBox1.Items.Add(e.KeyCode)
    e.Handled = False
    End Sub

    Public Shared Sub Main()
    Application.Run(New Form1)
    End Sub

    End Class


    ' This button is a simple extension of the button class that overrides
    ' the ProcessMnemonic method. If the mnemonic is correctly entered,
    ' the message box will appear and the click event will be raised.
    Public Class MyMnemonicButton
    Inherits Button

    ' This method makes sure the control is selectable and the
    ' mneumonic is correct before displaying the message box
    ' and triggering the click event.
    Protected Overrides Function ProcessMnemonic( _
    ByVal inputChar As Char) As Boolean

    If (CanSelect And IsMnemonic(inputChar, Me.Text)) Then
    MessageBox.Show("You've raised the click event " _
    & "using the mnemonic.")
    Me.PerformClick()
    Return True
    End If
    Return False
    End Function

    End Class

    [C#]
    using System.Windows.Forms;

    public class Form1:
    System.Windows.Forms.Form

    // Declare the controls contained on the form.
    {
    private MyMnemonicButton button1;
    internal System.Windows.Forms.ListBox ListBox1;

    public Form1() : base()
    {

    // Set KeyPreview object to true to allow the form to process
    // the key before the control with focus processes it.
    this.KeyPreview = true;

    // Add a MyMnemonicButton.
    button1 = new MyMnemonicButton();
    button1.Text = "&Click";
    button1.Location = new System.Drawing.Point(100, 120);
    this.Controls.Add(button1);

    // Initialize a ListBox control and the form itself.
    this.ListBox1 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    this.ListBox1.Location = new System.Drawing.Point(8, 8);
    this.ListBox1.Name = "ListBox1";
    this.ListBox1.Size = new System.Drawing.Size(120, 95);
    this.ListBox1.TabIndex = 0;
    this.ListBox1.Text = "Press a key";
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.ListBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    // Associate the event-handling method with the
    // KeyDown event.
    this.KeyDown += new KeyEventHandler(Form1_KeyDown);

    }

    // The form will handle all key events before the control with
    // focus handles them. Show the keys pressed by adding the
    // KeyCode object to ListBox1. Ensure the processing is passed
    // to the control with focus by setting the KeyEventArg.Handled
    // property to false.
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    ListBox1.Items.Add(e.KeyCode);
    e.Handled = false;
    }

    [System.STAThreadAttribute]
    public static void Main()
    {
    Application.Run(new Form1());
    }

    }


    // This button is a simple extension of the button class that overrides
    // the ProcessMnemonic method. If the mnemonic is correctly entered,
    // the message box will appear and the click event will be raised.
    public class MyMnemonicButton:
    Button

    // This method makes sure the control is selectable and the
    // mneumonic is correct before displaying the message box
    // and triggering the click event.
    {
    protected override bool ProcessMnemonic(char inputChar)
    {

    if (CanSelect&&IsMnemonic(inputChar, this.Text))
    {
    MessageBox.Show("You've raised the click event " +
    "using the mnemonic.");
    this.PerformClick();
    return true;
    }
    return false;
    }

    }

    [C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.

    Requirements
    Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

    See Also
    Form Class | Form Members | System.Windows.Forms Namespace | KeyPress | KeyDown | KeyUp | Form Members (Visual J# Syntax) | Managed Extensions for C++ Programming


    Syntax based on .NET Framework version 1.1.
    Documentation version 1.1.1.








  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2006-3-11 8:5:34[/red][/b][hr]

    [code]

    Private Sub formLevel_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

    Dim keyChr As Integer
    keyChr = e.KeyCode - 36
    Select Case keyChr
    Case 1
    TextBox1.Text = "LEFT"
    Case 2
    TextBox1.Text = "UP"
    Case 3
    TextBox1.Text = "RIGHT"
    Case 4
    TextBox1.Text = "DOWN"
    End Select

    End Sub
    [/code]

    [red][b]
    Don't forget to set the keyPreview property of your FORM to TRUE.

    PASTE this code after the line>>

    WINDOWS FORM DESIGNER GENERATED CODE
    and before the
    END CLASS
    line as usual. :-)

    [/b][/red]

    [blue]
    Regards,

    Dr M.
    [/blue]

Sign In or Register to comment.