key pressed

  • Thread starter Thread starter Mathieu Chavoutier
  • Start date Start date
M

Mathieu Chavoutier

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.
 
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]
 
"Morten Wennevik" <[email protected]> a écrit dans le message de Hi Mathieu,
Well, it is a bit unclear what kind of operations you want to cancel.

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.
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

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.
 
Mathieu Chavoutier said:
de


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.
that
is what you mean by C^z) like this

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

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)
 
Handling the parent form's KeyDown event should work fine.

Happy coding!
Morten Wennevik [C# MVP]
 
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(KeyEventArgs 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]
 
Morten Wennevik said:
But before you do that, try to use the parent form's keydown event and see
if that works.

I think i can make it with this.
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(KeyEventArgs e)
{
}

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.
 
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(object sender,
System.Windows.Forms.KeyEventArgs e)
{
}

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

this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

in the form's constructor.

Or you can override the keydown on the parent form using

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

Then put the code checking inside


private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs 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]
 
"Morten Wennevik" <[email protected]> a écrit dans le message de 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(object sender,
System.Windows.Forms.KeyEventArgs e)
{
}

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

this.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

in the form's constructor.

Or you can override the keydown on the parent form using

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

Then put the code checking inside

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

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.

I have to multi-thread ?
But, why does the mouse works with only one thread ?
 
"Morten Wennevik" <[email protected]> a écrit dans le message de Well, Mathieu,

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

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

this.KeyPress += new KeyPressEventHandler(keyPress);

this.KeyUp += new KeyEventHandler(keyDown);


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

{

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

}

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

{

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

}

It doesn't write anything :o/

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.

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))
 
Mathieu Chavoutier said:
I have this :
[...]
It doesn't write anything :o/

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 ?
 
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]
 
Back
Top