key pressed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mathieu Chavoutier

    key pressed

    Hi.

    I want to use esc key to cancel operations (I also want to use C^z). But, in
    "cancel", there is no escape character, and no z. So, how can I do to use
    those key ?

    Thanks.


  • Benny Mathew

    #2
    Re: key pressed

    Hi,
    Check if the 'CancelButton' property of the form will work for you.

    -Benny

    Mathieu Chavoutier wrote:
    [color=blue]
    > Hi.
    >
    > I want to use esc key to cancel operations (I also want to use C^z). But, in
    > "cancel", there is no escape character, and no z. So, how can I do to use
    > those key ?
    >
    > Thanks.
    >
    >[/color]

    Comment

    • Morten Wennevik

      #3
      Re: key pressed

      Hi Mathieu,

      Well, it is a bit unclear what kind of operations you want to cancel.
      You can use the KeyDown event and check for the escape or CTRL-Z (if that
      is what you mean by C^z) like this

      if(e.KeyCode == Keys.Escape)
      // escape key hit
      if(e.KeyCode == Keys.Z && e.Control)
      // z key hit while control is down = CTRL-Z


      Happy coding!
      Morten Wennevik [C# MVP]

      Comment

      • Mathieu Chavoutier

        #4
        Re: key pressed

        [color=blue]
        > "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de[/color]
        news:opr7r7oypw hntkfz@localhos t...[color=blue]
        > Hi Mathieu,[/color]
        [color=blue]
        > Well, it is a bit unclear what kind of operations you want to cancel.[/color]

        I know that, but if I write too much text, nobody will read :o)
        And, the operation that I have to cancel is different according where I am
        where the key is pressed.
        [color=blue]
        > You can use the KeyDown event and check for the escape or CTRL-Z (if that[/color]
        is what you mean by C^z) like this[color=blue]
        >
        > if(e.KeyCode == Keys.Escape)
        > // escape key hit
        > if(e.KeyCode == Keys.Z && e.Control)
        > // z key hit while control is down = CTRL-Z[/color]

        Okey, I was looking for it on a TabPage, and it was not there. But, because
        I also use panels, it will be Ok.

        Thank you very much.


        Comment

        • Morten Wennevik

          #5
          Re: key pressed

          Handling the parent form's KeyDown event should work fine.

          Happy coding!
          Morten Wennevik [C# MVP]

          Comment

          • Mathieu Chavoutier

            #6
            Re: key pressed


            "Mathieu Chavoutier" <nospam@no.spam > a écrit dans le message de
            news:u%23oEcroN EHA.1004@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            >[color=green]
            > > "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message[/color][/color]
            de[color=blue]
            > news:opr7r7oypw hntkfz@localhos t...[color=green]
            > > Hi Mathieu,[/color]
            >[color=green]
            > > Well, it is a bit unclear what kind of operations you want to cancel.[/color]
            >
            > I know that, but if I write too much text, nobody will read :o)
            > And, the operation that I have to cancel is different according where I am
            > where the key is pressed.
            >[color=green]
            > > You can use the KeyDown event and check for the escape or CTRL-Z (if[/color][/color]
            that[color=blue]
            > is what you mean by C^z) like this[color=green]
            > >
            > > if(e.KeyCode == Keys.Escape)
            > > // escape key hit
            > > if(e.KeyCode == Keys.Z && e.Control)
            > > // z key hit while control is down = CTRL-Z[/color]
            >
            > Okey, I was looking for it on a TabPage, and it was not there. But,[/color]
            because[color=blue]
            > I also use panels, it will be Ok.[/color]

            In fact, no, it is not ok : where is this event ?
            Isn't it like the resize event ?

            (for resize, there is a panel.resize. But, I can't find a panel.KeyDown)


            Comment

            • Morten Wennevik

              #7
              Re: key pressed

              To be able to handle a panel's or tabpage's KeyDown Event you need to
              create your own class and use

              protected override void OnKeyDown(KeyEv entArgs e)
              {
              }

              But before you do that, try to use the parent form's keydown event and see
              if that works.


              Happy coding!
              Morten Wennevik [C# MVP]

              Comment

              • Mathieu Chavoutier

                #8
                Re: key pressed

                > "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de
                news:opr7sbwndb hntkfz@localhos t...[color=blue]
                > But before you do that, try to use the parent form's keydown event and see
                > if that works.[/color]

                I think i can make it with this.
                [color=blue]
                > To be able to handle a panel's or tabpage's KeyDown Event you need to
                > create your own class and use
                >
                > protected override void OnKeyDown(KeyEv entArgs e)
                > {
                > }[/color]

                Because your code is here : if I create my class (let's call it MyKeyEvent
                :o)), how would I use it ? (Last time I tried to do so, it couldn't compile
                :o( )
                (I will use the form keyDown, but I'd like to know :o))

                Thank you for your help.


                Comment

                • Morten Wennevik

                  #9
                  Re: key pressed

                  Well, Mathieu,

                  If you use Visual Studio, doubleclick the keydown event for the parent
                  form. It will typically create something like this

                  private void Form1_KeyDown(o bject sender,
                  System.Windows. Forms.KeyEventA rgs e)
                  {
                  }

                  Or you can do it manually, in which case you need to add the line:

                  this.KeyDown += new
                  System.Windows. Forms.KeyEventH andler(this.For m1_KeyDown);

                  in the form's constructor.

                  Or you can override the keydown on the parent form using

                  protected override OnKeyDown(Syste m.Windows.Forms .KeyEventArgs e)
                  {
                  }

                  Then put the code checking inside


                  private void Form1_KeyDown(o bject sender,
                  System.Windows. Forms.KeyEventA rgs e)
                  {
                  if(e.KeyCode == Keys.Escape)
                  MessageBox.Show ("ESCAPE");
                  if(e.KeyCode == Keys.Z && e.Control)
                  MessageBox.Show ("CTRL-Z");
                  }

                  Then of course, if the job you want to cancel is happening in the same
                  thread, hitting any key combination won't work until the job is finished..
                  Unless you at regular intervals in your heavy duty working code check for
                  new events. Better yet is to put this code in a separate thread.

                  Happy coding!
                  Morten Wennevik [C# MVP]

                  Comment

                  • Mathieu Chavoutier

                    #10
                    Re: key pressed

                    [color=blue]
                    > "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de[/color]
                    news:opr7sc6tk4 hntkfz@localhos t...[color=blue]
                    > Well, Mathieu,
                    >
                    > If you use Visual Studio, doubleclick the keydown event for the parent
                    > form. It will typically create something like this
                    >
                    > private void Form1_KeyDown(o bject sender,
                    > System.Windows. Forms.KeyEventA rgs e)
                    > {
                    > }
                    >
                    > Or you can do it manually, in which case you need to add the line:
                    >
                    > this.KeyDown += new
                    > System.Windows. Forms.KeyEventH andler(this.For m1_KeyDown);
                    >
                    > in the form's constructor.
                    >
                    > Or you can override the keydown on the parent form using
                    >
                    > protected override OnKeyDown(Syste m.Windows.Forms .KeyEventArgs e)
                    > {
                    > }
                    >
                    > Then put the code checking inside
                    >
                    > private void Form1_KeyDown(o bject sender,
                    > System.Windows. Forms.KeyEventA rgs e)
                    > {
                    > if(e.KeyCode == Keys.Escape)
                    > MessageBox.Show ("ESCAPE");
                    > if(e.KeyCode == Keys.Z && e.Control)
                    > MessageBox.Show ("CTRL-Z");
                    > }[/color]

                    Oki.
                    [color=blue]
                    > Then of course, if the job you want to cancel is happening in the same
                    > thread, hitting any key combination won't work until the job is finished.
                    > Unless you at regular intervals in your heavy duty working code check for
                    > new events. Better yet is to put this code in a separate thread.[/color]

                    I have to multi-thread ?
                    But, why does the mouse works with only one thread ?


                    Comment

                    • Mathieu Chavoutier

                      #11
                      Re: key pressed

                      [color=blue]
                      > "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de[/color]
                      news:opr7sc6tk4 hntkfz@localhos t...[color=blue]
                      > Well, Mathieu,
                      >
                      > private void Form1_KeyDown(o bject sender,
                      > System.Windows. Forms.KeyEventA rgs e)
                      > {
                      > if(e.KeyCode == Keys.Escape)
                      > MessageBox.Show ("ESCAPE");
                      > if(e.KeyCode == Keys.Z && e.Control)
                      > MessageBox.Show ("CTRL-Z");
                      > }[/color]

                      I have this :
                      this.KeyDown += new KeyEventHandler (keyDown);

                      this.KeyPress += new KeyPressEventHa ndler(keyPress) ;

                      this.KeyUp += new KeyEventHandler (keyDown);


                      Then :
                      private void keyPress(object sender, System.Windows. Forms.KeyPressE ventArgs
                      e)

                      {

                      System.Console. WriteLine("Pres s -> " + e.KeyChar);

                      }

                      private void keyDown(object sender, System.Windows. Forms.KeyEventA rgs e)

                      {

                      System.Console. WriteLine("Othe r -> " + e.KeyValue);

                      }

                      It doesn't write anything :o/

                      [color=blue]
                      > Then of course, if the job you want to cancel is happening in the same
                      > thread, hitting any key combination won't work until the job is finished.
                      > Unless you at regular intervals in your heavy duty working code check for
                      > new events. Better yet is to put this code in a separate thread.[/color]

                      In fact, I (will) have a plain window, and somebody will draw on it (with
                      the mouse). When he would like to cancel a draw (a part), he will C^z or
                      esc, and I will move backward.
                      So, I don't have to use thread, isn't it ? (it a question :o))


                      Comment

                      • Mathieu Chavoutier

                        #12
                        Re: key pressed


                        "Mathieu Chavoutier" <nospam@no.spam > a écrit dans le message de
                        news:%23P0lvGqN EHA.3492@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                        > I have this :
                        > [...]
                        > It doesn't write anything :o/[/color]

                        I have found why : the form is filled with a TabControl. So, I have to use
                        the one of the TabControl, not of the main.

                        But, why the TabControl have a KeyPress/Down/Up, but not the Panel ?


                        Comment

                        • Morten Wennevik

                          #13
                          Re: key pressed

                          The panel has key events too, but you have to override them in an
                          inherited class. Couldn't tell why.

                          Happy coding!
                          Morten Wennevik [C# MVP]

                          Comment

                          • Mathieu Chavoutier

                            #14
                            Re: key pressed


                            "Morten Wennevik" <MortenWennevik @hotmail.com> a écrit dans le message de
                            news:opr7tlcvjt hntkfz@localhos t...[color=blue]
                            > The panel has key events too, but you have to override them in an
                            > inherited class. Couldn't tell why.[/color]

                            Oki.

                            Well, thank you :o)


                            Comment

                            Working...