473,385 Members | 2,029 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,385 software developers and data experts.

KeyUp events

Ben
Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben
Jun 24 '07 #1
11 5633

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
Hello,

I'm trying to catch the pressing of the left and right arrow keys through
the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?
I think you will need to use e.Keyvalue.

>
Thanks,
Ben
Jun 24 '07 #2
Ben
Mr. Arnold wrote:
>
"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
>Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting
the event binding in the form load, but that didn't work either.

Any ideas?

I think you will need to use e.Keyvalue.

>>
Thanks,
Ben

That's not the problem. It doesn't even stop executing when putting a
breakpoint on the first line of the function, which leads me to believe
it's not even called.
Jun 24 '07 #3
May be the event is not reaching the form? Do you get the same problem when
you have an empty form without any child controls?

SG

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
Hello,

I'm trying to catch the pressing of the left and right arrow keys through
the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben

Jun 24 '07 #4
Ben
Gugale at Lincoln wrote:
May be the event is not reaching the form? Do you get the same problem when
you have an empty form without any child controls?

SG

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
>Hello,

I'm trying to catch the pressing of the left and right arrow keys through
the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben

That doesn't work either. I created a whole new project with just an
empty form using about the same code and no luck.
Jun 24 '07 #5
Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_Ke yUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

Console.WriteLine(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail.comwrote in message
news:OU**************@TK2MSFTNGP06.phx.gbl...
Gugale at Lincoln wrote:
>May be the event is not reaching the form? Do you get the same problem
when you have an empty form without any child controls?

SG

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
>>Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben


That doesn't work either. I created a whole new project with just an empty
form using about the same code and no luck.

Jun 24 '07 #6
Ben
Gugale at Lincoln wrote:
Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_Ke yUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

Console.WriteLine(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail.comwrote in message
news:OU**************@TK2MSFTNGP06.phx.gbl...
>Gugale at Lincoln wrote:
>>May be the event is not reaching the form? Do you get the same problem
when you have an empty form without any child controls?

SG

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben
That doesn't work either. I created a whole new project with just an empty
form using about the same code and no luck.

I just tried that, like so:

public Form1()
{
InitializeComponent();
this.KeyUp += new KeyEventHandler(this.ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
MessageBox.Show("Left arrow key");
else if (e.KeyData == Keys.Right)
MessageBox.Show("Right arrow key");
}

But still no luck.

Odd.
Jun 24 '07 #7
Have you renamed the form to ActiveForm? Can you post the entire code?

SG

"Ben" <el*****@gmail.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
Gugale at Lincoln wrote:
>Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_Ke yUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

Console.WriteLine(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail.comwrote in message
news:OU**************@TK2MSFTNGP06.phx.gbl...
>>Gugale at Lincoln wrote:
May be the event is not reaching the form? Do you get the same problem
when you have an empty form without any child controls?

SG

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
Hello,
>
I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:
>
public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}
>
void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}
>
Yet the event is never called when I press a button. I tried putting
the event binding in the form load, but that didn't work either.
>
Any ideas?
>
Thanks,
Ben

That doesn't work either. I created a whole new project with just an
empty form using about the same code and no luck.


I just tried that, like so:

public Form1()
{
InitializeComponent();
this.KeyUp += new KeyEventHandler(this.ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
MessageBox.Show("Left arrow key");
else if (e.KeyData == Keys.Right)
MessageBox.Show("Right arrow key");
}

But still no luck.

Odd.

Jun 24 '07 #8
Ben
It's just a function name, so it shouldn't matter.
Here's the full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using Routrek.SSHC;
using Routrek.SSHCV2;

namespace ComicViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyUp += new KeyEventHandler(this.ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
MessageBox.Show("Left arrow key");
else if (e.KeyData == Keys.Right)
MessageBox.Show("Right arrow key");
}

DateTime curdate = new DateTime(1978, 06, 19);
DateTime begindate;
int totaldays = 0;

private void Form1_Load(object sender, EventArgs e)
{
loadImage(curdate);

setDate();

// Populate progressbar
TimeSpan numdays = DateTime.Now - curdate;
totaldays = numdays.Days;
begindate = curdate;
progressBar1.Maximum = numdays.Days;
progressBar1.Minimum = 0;

setProgressBar();
}

public void setDate()
{
textBox1.Text = curdate.Year.ToString();
textBox2.Text = curdate.Month.ToString();
textBox3.Text = curdate.Day.ToString();
}

public void setProgressBar()
{
TimeSpan span = curdate - begindate;
progressBar1.Value = span.Days;
toolTip1.SetToolTip(progressBar1, "Comic " + span.Days +
"/" + totaldays);
}

public string formatDate(DateTime time)
{
string year = curdate.Year.ToString();
year = year.Substring(year.Length - 2);
string month = curdate.Month.ToString().PadLeft(2, '0');
string day = curdate.Day.ToString().PadLeft(2, '0');

return year + month + day;
}

public void loadImage(DateTime time)
{
try
{
HttpWebRequest req =
(HttpWebRequest)HttpWebRequest.Create("http://192.168.1.12/garfield/garfield_coll/ga"
+ formatDate(time) + ".gif");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
pictureBox1.Image =
Image.FromStream(res.GetResponseStream());
}
catch (Exception e)
{
button1_Click(new object(), new EventArgs());
}
}

private void button2_Click(object sender, EventArgs e)
{
curdate = curdate.AddDays(1);
progressBar1.Value += 1;
loadImage(curdate);

setDate();
setProgressBar();
}

private void button1_Click(object sender, EventArgs e)
{
curdate = curdate.AddDays(-1);
progressBar1.Value -= 1;
loadImage(curdate);

setDate();
setProgressBar();
}

private void button3_Click(object sender, EventArgs e)
{
curdate = DateTime.Parse(textBox1.Text + "/" +
textBox2.Text + "/" + textBox3.Text);
loadImage(curdate);

setDate();
setProgressBar();
}

private void button4_Click(object sender, EventArgs e)
{
using (showLink frm = new showLink())
{
frm.textBox1.Text =
"http://images.ucomics.com/comics/ga/" + curdate.Year + "/ga" +
formatDate(curdate) + ".gif";
frm.textBox1.Update();
frm.ShowDialog();
}
}
}
}

Gugale at Lincoln wrote:
Have you renamed the form to ActiveForm? Can you post the entire code?

SG

"Ben" <el*****@gmail.comwrote in message
news:ek**************@TK2MSFTNGP06.phx.gbl...
>Gugale at Lincoln wrote:
>>Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form2_Ke yUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

Console.WriteLine(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail.comwrote in message
news:OU**************@TK2MSFTNGP06.phx.gbl...
Gugale at Lincoln wrote:
May be the event is not reaching the form? Do you get the same problem
when you have an empty form without any child controls?
>
SG
>
"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl.. .
>Hello,
>>
>I'm trying to catch the pressing of the left and right arrow keys
>through the KeyUp event, like so:
>>
>public Form1()
> {
> InitializeComponent();
> Form1.ActiveForm.KeyUp += new
>KeyEventHandler(ActiveForm_KeyUp);
> }
>>
>void ActiveForm_KeyUp(object sender, KeyEventArgs e)
> {
> if (e.KeyCode == Keys.Left)
> MessageBox.Show("Left arrow key");
> else if(e.KeyCode == Keys.Right)
> MessageBox.Show("Right arrow key");
> }
>>
>Yet the event is never called when I press a button. I tried putting
>the event binding in the form load, but that didn't work either.
>>
>Any ideas?
>>
>Thanks,
>Ben
That doesn't work either. I created a whole new project with just an
empty form using about the same code and no luck.
I just tried that, like so:

public Form1()
{
InitializeComponent();
this.KeyUp += new KeyEventHandler(this.ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
MessageBox.Show("Left arrow key");
else if (e.KeyData == Keys.Right)
MessageBox.Show("Right arrow key");
}

But still no luck.

Odd.

Jun 24 '07 #9
Ben wrote:
Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.
Is the form's KeyPreview variable set to true?

Alun Harford
Jun 24 '07 #10
Ben
Alun Harford wrote:
Ben wrote:
>Hello,

I'm trying to catch the pressing of the left and right arrow keys
through the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting
the event binding in the form load, but that didn't work either.

Is the form's KeyPreview variable set to true?

Alun Harford
Ah, that did the trick, thanks :D
Jun 24 '07 #11
Hi,

All you need is to set the "KeyPreview" property of the form to "TRUE". This
enables the Key events to be received in the form level.

This along with the below code should work just fine:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.KeyUp += new KeyEventHandler(Form1_KeyUp);

//
// TODO: Add any constructor code after InitializeComponent call
//
}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");

}

Hope this helps!
Thanks -

"Ben" <el*****@gmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
Hello,

I'm trying to catch the pressing of the left and right arrow keys through
the KeyUp event, like so:

public Form1()
{
InitializeComponent();
Form1.ActiveForm.KeyUp += new
KeyEventHandler(ActiveForm_KeyUp);
}

void ActiveForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
MessageBox.Show("Left arrow key");
else if(e.KeyCode == Keys.Right)
MessageBox.Show("Right arrow key");
}

Yet the event is never called when I press a button. I tried putting the
event binding in the form load, but that didn't work either.

Any ideas?

Thanks,
Ben

Jun 25 '07 #12

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

Similar topics

34
by: Andrew DeFaria | last post by:
I thought this would be fairly straight forward but apparently it's not. Given the following html file: <!DOCTYPE html PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html> <head>...
3
by: Sharon | last post by:
I’m using a DataGrid control on my form; and I want to handle the KeyUp event. So I did: myDataGrid.KeyUp += new KeyEventHandler(this.OnKeyUp); When the DataGrid is empty, I mean when It has...
2
by: ZS | last post by:
Hi, On a form , I'm trying to trap when a shift key is pressed. Can someone explain how the KeyUp,KeyDown and Key Press event works for Forms. Thanks -ZS
4
by: Serdge Kooleman | last post by:
How to get "Key press" event when i'm working with DataGrid (winforms). Seems to me that standard events "KeyUp, KeyPress" are ignored :-( Thank you
0
by: Gene Hubert | last post by:
I'm trying to catch the KeyUp event in textbox of a DataGrid. I'm picking up the keydown and keypress events ok, but not keyup. Can anyone see what is wrong with this code. I been fighting with...
2
by: Adam J. Schaff | last post by:
I have recently noticed an unwanted behavior that I do not know how to get rid of. To Recreate Problem: Windows Forms App with 2 forms. Form 1 has nothing on it and this code underneath: ...
4
by: **Developer** | last post by:
I have a usercontrol that contains the following. To my surprise the form containing this control get KeyUp events. Help says that for KeyPress setting e.Handled = True suppresses KeyPress...
4
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.