Arrow Keys neglected on User Control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martijn Mulder

    #1

    Arrow Keys neglected on User Control

    /*
    I have problems detecting the Arrow Keys on a User Control. A control
    derived from System.Windows. Forms.Control neglects 'bare' Arrow Keys
    but does react on the combination <Altor <Ctrl+ Arrow Key. The code
    below shows what I mean. How can I cure this? (excuse me for the line
    breaks)
    */




    //class KeyTest
    class KeyTest:System. Windows.Forms.C ontrol
    {


    //data member label
    System.Windows. Forms.Label label=new System.Windows. Forms.Label();


    //constructor
    KeyTest()
    {
    Controls.Add(la bel);
    KeyDown+=OnKeyD own;
    Text="Arrow Keys are Special?";
    label.Text="\nP ress <1>...";
    Dock=System.Win dows.Forms.Dock Style.Fill;
    label.Dock=Syst em.Windows.Form s.DockStyle.Fil l;
    }


    //OnKeyDown
    void OnKeyDown(objec t a,System.Window s.Forms.KeyEven tArgs b)
    {
    switch(b.KeyCod e)
    {
    case System.Windows. Forms.Keys.D1:
    label.Text+="\n You pressed 1."+
    "\n\nNow hold down <Altor <Ctrland press the Arrow Up Key...";
    break;
    case System.Windows. Forms.Keys.Up:
    label.Text+="\n You pressed the Arrow Up Key."+
    "\n\nNow release <Altor <Ctrland press the Arrow Up Key
    again..."+
    "\n\nIt doesn't work!"+
    "\nPress <Escor <Spaceto Quit";
    break;
    case System.Windows. Forms.Keys.Esca pe:
    System.Windows. Forms.Applicati on.Exit();
    break;
    case System.Windows. Forms.Keys.Spac e:
    System.Windows. Forms.Applicati on.Exit();
    break;
    }
    }


    //Main
    [System.STAThrea d]
    static void Main()
    {
    System.Windows. Forms.Form form=new System.Windows. Forms.Form();
    form.Controls.A dd(new KeyTest());
    System.Windows. Forms.Applicati on.Run(form);
    }
    }



  • Peter Thornqvist

    #2
    Re: Arrow Keys neglected on User Control

    override IsInputKey and do something like this:

    protected override bool IsInputKey(Syst em.Windows.Form s.Keys
    keyData)
    {
    switch (keyData & Keys.KeyCode)
    {
    case Keys.Up:
    return true;
    case Keys.Down:
    return true;
    case Keys.Right:
    return true;
    case Keys.Left:
    return true;
    default:
    return base.IsInputKey (keyData);
    }

    }

    --

    Regards, Peter

    "Martijn Mulder" <i@mskrev i meddelandet
    news:454130e1$0 $77254$dbd49001 @news.wanadoo.n l...
    /*
    I have problems detecting the Arrow Keys on a User Control. A control
    derived from System.Windows. Forms.Control neglects 'bare' Arrow Keys
    but does react on the combination <Altor <Ctrl+ Arrow Key. The code
    below shows what I mean. How can I cure this? (excuse me for the line
    breaks)
    */
    >
    >
    >
    >
    //class KeyTest
    class KeyTest:System. Windows.Forms.C ontrol
    {
    >
    >
    //data member label
    System.Windows. Forms.Label label=new System.Windows. Forms.Label();
    >
    >
    //constructor
    KeyTest()
    {
    Controls.Add(la bel);
    KeyDown+=OnKeyD own;
    Text="Arrow Keys are Special?";
    label.Text="\nP ress <1>...";
    Dock=System.Win dows.Forms.Dock Style.Fill;
    label.Dock=Syst em.Windows.Form s.DockStyle.Fil l;
    }
    >
    >
    //OnKeyDown
    void OnKeyDown(objec t a,System.Window s.Forms.KeyEven tArgs b)
    {
    switch(b.KeyCod e)
    {
    case System.Windows. Forms.Keys.D1:
    label.Text+="\n You pressed 1."+
    "\n\nNow hold down <Altor <Ctrland press the Arrow Up Key...";
    break;
    case System.Windows. Forms.Keys.Up:
    label.Text+="\n You pressed the Arrow Up Key."+
    "\n\nNow release <Altor <Ctrland press the Arrow Up Key
    again..."+
    "\n\nIt doesn't work!"+
    "\nPress <Escor <Spaceto Quit";
    break;
    case System.Windows. Forms.Keys.Esca pe:
    System.Windows. Forms.Applicati on.Exit();
    break;
    case System.Windows. Forms.Keys.Spac e:
    System.Windows. Forms.Applicati on.Exit();
    break;
    }
    }
    >
    >
    //Main
    [System.STAThrea d]
    static void Main()
    {
    System.Windows. Forms.Form form=new System.Windows. Forms.Form();
    form.Controls.A dd(new KeyTest());
    System.Windows. Forms.Applicati on.Run(form);
    }
    }
    >
    >
    >

    Comment

    Working...