472,142 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 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 5514

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sharon | last post: by
2 posts views Thread by ZS | last post: by
reply views Thread by Gene Hubert | last post: by
2 posts views Thread by Adam J. Schaff | last post: by
4 posts views Thread by ShaneO | last post: by
2 posts views Thread by Tony Johansson | last post: by
reply views Thread by leo001 | last post: by

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.