473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Nov 16 '05 #1
13 19767
Hi,
Check if the 'CancelButton' property of the form will work for you.

-Benny

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


Nov 16 '05 #2
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]
Nov 16 '05 #3
"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de news:opr7r7oypwhntkfz@localhost... 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.
Nov 16 '05 #4
Handling the parent form's KeyDown event should work fine.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

"Mathieu Chavoutier" <no****@no.spam> a écrit dans le message de
news:u%****************@TK2MSFTNGP10.phx.gbl...
"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message
de news:opr7r7oypwhntkfz@localhost...
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.


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)
Nov 16 '05 #6
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]
Nov 16 '05 #7
> "Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr7sbwndbhntkfz@localhost...
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.
Nov 16 '05 #8
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_Ke yDown);

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]
Nov 16 '05 #9
"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de news:opr7sc6tk4hntkfz@localhost... 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_Ke yDown);

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 ?
Nov 16 '05 #10
"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de news:opr7sc6tk4hntkfz@localhost... 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))
Nov 16 '05 #11

"Mathieu Chavoutier" <no****@no.spam> a écrit dans le message de
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 ?
Nov 16 '05 #12
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]
Nov 16 '05 #13

"Morten Wennevik" <Mo************@hotmail.com> a écrit dans le message de
news:opr7tlcvjthntkfz@localhost...
The panel has key events too, but you have to override them in an
inherited class. Couldn't tell why.


Oki.

Well, thank you :o)
Nov 16 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rune | last post by:
Hey I'm trying to build a gui application and need to know if the user is actually holding down the shift or ctrl key. That is, if the user currently is holding down the shift key. In pseudo...
0
by: Koen | last post by:
Hi! What is the best way to check whether a key is pressed without blocking in C++? I know I can use cin.get(c) to get a character from standard input, but this blocks. And I know _kbhit()...
2
by: jb | last post by:
Hello, I need to know which button was pressed in the submit , i tried reading the vaule of submit it the validateDate function but it returns 'undefined' value ; I do this in asp all the time, Not...
8
by: Syed Ali | last post by:
Hello, I have 1 HTML form with 4 submit buttons and 10 textfield entry areas. If submit button1 is pressed I need to make sure that all 10 textfield entries have been filled before submitting...
3
by: James McGivney | last post by:
I have a project in VS.NET using C# I have a series of buttons on an aspx page. When one of the buttons is pressed, a panel becomes visible and allows the user to enter and edit data. I want to...
4
by: actionwoman63 | last post by:
Dear all I need to be able to check which one out of two submit buttons within the same form was pressed in a javascript function prior to form submission. And before I get flamed for not...
3
by: Tegdeep | last post by:
Here's what I want to do: I have a hash table which contains data associated to different keys. The Hash keys are represented by a single character from the keyboard: 0-9, a-z, A-Z, and the...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
6
by: Stefan Mueller | last post by:
With the following code I can figure out if 'Enter' has been pressed: ... onKeyPress = "if (check_enter(event)) {alert('Enter has been pressed');}" function check_enter(eventobjekt) { var...
2
by: matthewr | last post by:
In Internet Explorer, for example, when you hit return in the address bar, the Go button is pressed. In my program, I have a toolstrip with a textbox and button. How do I ensure the button is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.