KeyUp events

B

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
 
M

Mr. Arnold

Ben said:
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.
 
B

Ben

Mr. Arnold said:
I think you will need to use e.Keyvalue.


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

Gugale at Lincoln

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
 
B

Ben

Gugale said:
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

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

Gugale at Lincoln

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_KeyUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

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

}

}
 
B

Ben

Gugale said:
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_KeyUp);

}

private void Form2_KeyUp(object sender, KeyEventArgs e)

{

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

}

}

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

Gugale at Lincoln

Have you renamed the form to ActiveForm? Can you post the entire code?

SG
 
B

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();
}
}
}
}
 
A

Alun Harford

Ben said:
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
 
S

Shine Xavier

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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top