473,796 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandler (ActiveForm_Key Up);
}

void ActiveForm_KeyU p(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 5665

"Ben" <el*****@gmail. comwrote in message
news:ud******** ******@TK2MSFTN GP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandler (ActiveForm_Key Up);
}

void ActiveForm_KeyU p(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******** ******@TK2MSFTN GP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandle r(ActiveForm_Ke yUp);
}

void ActiveForm_KeyU p(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******** ******@TK2MSFTN GP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandler (ActiveForm_Key Up);
}

void ActiveForm_KeyU p(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******** ******@TK2MSFTN GP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandle r(ActiveForm_Ke yUp);
}

void ActiveForm_KeyU p(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()

{

InitializeCompo nent();

this.KeyUp += new System.Windows. Forms.KeyEventH andler(this.For m2_KeyUp);

}

private void Form2_KeyUp(obj ect sender, KeyEventArgs e)

{

Console.WriteLi ne(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail. comwrote in message
news:OU******** ******@TK2MSFTN GP06.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******* *******@TK2MSFT NGP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandl er(ActiveForm_K eyUp);
}

void ActiveForm_KeyU p(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()

{

InitializeCompo nent();

this.KeyUp += new System.Windows. Forms.KeyEventH andler(this.For m2_KeyUp);

}

private void Form2_KeyUp(obj ect sender, KeyEventArgs e)

{

Console.WriteLi ne(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail. comwrote in message
news:OU******** ******@TK2MSFTN GP06.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****** ********@TK2MSF TNGP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHand ler(ActiveForm_ KeyUp);
}

void ActiveForm_KeyU p(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()
{
InitializeCompo nent();
this.KeyUp += new KeyEventHandler (this.ActiveFor m_KeyUp);
}

void ActiveForm_KeyU p(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******** ******@TK2MSFTN GP06.phx.gbl...
Gugale at Lincoln wrote:
>Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeComp onent();

this.KeyUp += new System.Windows. Forms.KeyEventH andler(this.For m2_KeyUp);

}

private void Form2_KeyUp(obj ect sender, KeyEventArgs e)

{

Console.WriteL ine(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail. comwrote in message
news:OU******* *******@TK2MSFT NGP06.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***** *********@TK2MS FTNGP06.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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHan dler(ActiveForm _KeyUp);
}
>
void ActiveForm_KeyU p(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()
{
InitializeCompo nent();
this.KeyUp += new KeyEventHandler (this.ActiveFor m_KeyUp);
}

void ActiveForm_KeyU p(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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Net.Sock ets;
using System.IO;
using System.Text.Reg ularExpressions ;
using System.Web;
using System.Net;
using Routrek.SSHC;
using Routrek.SSHCV2;

namespace ComicViewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
this.KeyUp += new KeyEventHandler (this.ActiveFor m_KeyUp);
}

void ActiveForm_KeyU p(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(obje ct sender, EventArgs e)
{
loadImage(curda te);

setDate();

// Populate progressbar
TimeSpan numdays = DateTime.Now - curdate;
totaldays = numdays.Days;
begindate = curdate;
progressBar1.Ma ximum = numdays.Days;
progressBar1.Mi nimum = 0;

setProgressBar( );
}

public void setDate()
{
textBox1.Text = curdate.Year.To String();
textBox2.Text = curdate.Month.T oString();
textBox3.Text = curdate.Day.ToS tring();
}

public void setProgressBar( )
{
TimeSpan span = curdate - begindate;
progressBar1.Va lue = span.Days;
toolTip1.SetToo lTip(progressBa r1, "Comic " + span.Days +
"/" + totaldays);
}

public string formatDate(Date Time time)
{
string year = curdate.Year.To String();
year = year.Substring( year.Length - 2);
string month = curdate.Month.T oString().PadLe ft(2, '0');
string day = curdate.Day.ToS tring().PadLeft (2, '0');

return year + month + day;
}

public void loadImage(DateT ime time)
{
try
{
HttpWebRequest req =
(HttpWebRequest )HttpWebRequest .Create("http://192.168.1.12/garfield/garfield_coll/ga"
+ formatDate(time ) + ".gif");
HttpWebResponse res = (HttpWebRespons e)req.GetRespon se();
pictureBox1.Ima ge =
Image.FromStrea m(res.GetRespon seStream());
}
catch (Exception e)
{
button1_Click(n ew object(), new EventArgs());
}
}

private void button2_Click(o bject sender, EventArgs e)
{
curdate = curdate.AddDays (1);
progressBar1.Va lue += 1;
loadImage(curda te);

setDate();
setProgressBar( );
}

private void button1_Click(o bject sender, EventArgs e)
{
curdate = curdate.AddDays (-1);
progressBar1.Va lue -= 1;
loadImage(curda te);

setDate();
setProgressBar( );
}

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

setDate();
setProgressBar( );
}

private void button4_Click(o bject sender, EventArgs e)
{
using (showLink frm = new showLink())
{
frm.textBox1.Te xt =
"http://images.ucomics. com/comics/ga/" + curdate.Year + "/ga" +
formatDate(curd ate) + ".gif";
frm.textBox1.Up date();
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******** ******@TK2MSFTN GP06.phx.gbl...
>Gugale at Lincoln wrote:
>>Use KeyData I tried following code and it works.

SG
public partial class Form2 : Form

{

public Form2()

{

InitializeCom ponent();

this.KeyUp += new System.Windows. Forms.KeyEventH andler(this.For m2_KeyUp);

}

private void Form2_KeyUp(obj ect sender, KeyEventArgs e)

{

Console.Write Line(e.KeyData == Keys.Left);

}

}

"Ben" <el*****@gmail. comwrote in message
news:OU****** ********@TK2MSF TNGP06.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**** **********@TK2M SFTNGP06.phx.gb l...
>Hello,
>>
>I'm trying to catch the pressing of the left and right arrow keys
>through the KeyUp event, like so:
>>
>public Form1()
> {
> InitializeCompo nent();
> Form1.ActiveFor m.KeyUp += new
>KeyEventHa ndler(ActiveFor m_KeyUp);
> }
>>
>void ActiveForm_KeyU p(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()
{
InitializeCompo nent();
this.KeyUp += new KeyEventHandler (this.ActiveFor m_KeyUp);
}

void ActiveForm_KeyU p(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()
{
InitializeCompo nent();
Form1.ActiveFor m.KeyUp += new
KeyEventHandler (ActiveForm_Key Up);
}

void ActiveForm_KeyU p(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

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

Similar topics

34
4929
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> <title>Test</title> </head> <body> <form method="post" action="javascript:">
3
3122
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 no data so now rows are seen, all keys are causing my this.OnKeyUp(...) to be invoked correctly. But when the DataGrid is populated with some data (rows are seen with only DataGridTextBoxes), only several keys are causing it to be invoked.
2
4109
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
8498
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
1420
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 this for way too long. Thanks a ton, Gene H. ==================
2
2355
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: Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Return Then
4
6981
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 events but say very little about KeyUp. What does setting to Handled do?
4
7131
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 System.Windows.Forms.KeyEventArgs) _ Handles ListBox1.KeyUp, ListBox1.KeyDown If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or e.KeyValue = Keys.End Or e.KeyValue = Keys.Home Then
2
19299
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 have created for this purpose is derived from class UserControl.
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.