R
raylopez99
KeyDown won't work KeyPress fails KeyDown not seen
inspired by a poster here:http://tinyurl.com/62d97l I found some
interesting stuff, which I reproduce below for newbies like me.
The main reason you would want to do this is for example to trigger
something from an OnPaint event without resorting to boolean switches--
say if a user presses the "M" key while the program is Painting, the
user gets the PaintHandler to do something else. Arguably it's
quicker than waiting for Paint to finish, though I'm not sure of
that. BTW I think for KeyPress the form needs to have focus, but I'm
not sure. But KeyDown I think will work regardless of focus.
RL
public partial class Form1 : Form
{
Keys myGlobalKey;
char myGlobalChar; //declare your 'global' (not really)
variables here--important!
// stuff deleted
public Form1()
{
myGlobalKey = new Keys(); //instantiate here. For safety
do this before InitializeComponent();
myGlobalKey2 = new Keys();
myGlobalChar = new char();
//stuff deleted
// !!! IMPORTANT – make sure “key preview” property is set to true in
Form!! Default is False (!)
// InitializeComponent(); should have this line (if key preview
property set to true): this.KeyPreview = true
// for Debug. to work below (optional) set debugger windows to operate
(F5 key) and declare “using System.Diagnostics;”
///////// set up “key down” event (lightning bolt)
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
myGlobalKey = e.KeyCode;
if (myGlobalKey == Keys.D)
{
Debug.WriteLine("D/d pressed");
MessageBox.Show("D!");
} //works fine from here, for example, but also works from
OnPaint, see below
Invalidate(true); //refresh the form immeadiately after a
key pressed
}
// for less bounce (debatable) this is an older way of getting
information from the key, use “KeyPress” event (works with char not
KeyCode). Not as easy to use most say.
private void Form1_KeyPress(object sender, KeyPressEventArgs
e)
{
//more stable, since key down and up both req'd, but need
control to have focus
myGlobalChar = e.KeyChar; //not e.KeyCode, but returns
character
if (myGlobalChar== 'x')
{
Debug.WriteLine("small X pressed");
MessageBox.Show("x!"); //works fine from here
}
Invalidate(true);
}
////////// now, to show keypresses work even from OnPaint, which is
overridden onto the base PaintEventHandler
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); //!!! manditory or nothing from Paint Event
handler done. Putting it at beginning rather than end of OnPaint
executes Paint event handler first rather than last (before rather
than after OnPaint). Note: Frazier p. 450 to p453 has a discussion on
OnPaint versus PaintEventHandler base. base.OnPaint(e) will trigger
the PaintEventHandler
if (MouseButtons == MouseButtons.Left)
{
Debug.WriteLine("LMB clicked");
}
//shows you can get mouse info from inside OnPaint
if (myGlobalKey == Keys.O)
{
Debug.WriteLine("Oh! O pressed inside paint");
MessageBox.Show("O! from Paint");
} //works fine, but note: since OnPaint refreshes so fast, you’ll
get a dozen or so messageboxes even if you press quickly. Use escape
key to get out of them
if (myGlobalChar == 'w')
{
Debug.WriteLine("small w pressed Paint");
MessageBox.Show("w! from Paint"); //works fine, but you get lots
of messageboxes, as before
}
// another way of triggering keyboard using ModifierKeys
if ((ModifierKeys == Keys.Shift))
{
Debug.WriteLine("Shift pressed");
if (Cursor.Position.X > 50)
{ MessageBox.Show (“shift pressed and cursor > 50”);}
} //shows you can get/use mouse cursor from inside OnPaint
// works fine, but I could not get documentation on how ModifierKeys
is declared, so I prefer to use Keys as above.
inspired by a poster here:http://tinyurl.com/62d97l I found some
interesting stuff, which I reproduce below for newbies like me.
The main reason you would want to do this is for example to trigger
something from an OnPaint event without resorting to boolean switches--
say if a user presses the "M" key while the program is Painting, the
user gets the PaintHandler to do something else. Arguably it's
quicker than waiting for Paint to finish, though I'm not sure of
that. BTW I think for KeyPress the form needs to have focus, but I'm
not sure. But KeyDown I think will work regardless of focus.
RL
public partial class Form1 : Form
{
Keys myGlobalKey;
char myGlobalChar; //declare your 'global' (not really)
variables here--important!
// stuff deleted
public Form1()
{
myGlobalKey = new Keys(); //instantiate here. For safety
do this before InitializeComponent();
myGlobalKey2 = new Keys();
myGlobalChar = new char();
//stuff deleted
// !!! IMPORTANT – make sure “key preview” property is set to true in
Form!! Default is False (!)
// InitializeComponent(); should have this line (if key preview
property set to true): this.KeyPreview = true
// for Debug. to work below (optional) set debugger windows to operate
(F5 key) and declare “using System.Diagnostics;”
///////// set up “key down” event (lightning bolt)
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
myGlobalKey = e.KeyCode;
if (myGlobalKey == Keys.D)
{
Debug.WriteLine("D/d pressed");
MessageBox.Show("D!");
} //works fine from here, for example, but also works from
OnPaint, see below
Invalidate(true); //refresh the form immeadiately after a
key pressed
}
// for less bounce (debatable) this is an older way of getting
information from the key, use “KeyPress” event (works with char not
KeyCode). Not as easy to use most say.
private void Form1_KeyPress(object sender, KeyPressEventArgs
e)
{
//more stable, since key down and up both req'd, but need
control to have focus
myGlobalChar = e.KeyChar; //not e.KeyCode, but returns
character
if (myGlobalChar== 'x')
{
Debug.WriteLine("small X pressed");
MessageBox.Show("x!"); //works fine from here
}
Invalidate(true);
}
////////// now, to show keypresses work even from OnPaint, which is
overridden onto the base PaintEventHandler
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); //!!! manditory or nothing from Paint Event
handler done. Putting it at beginning rather than end of OnPaint
executes Paint event handler first rather than last (before rather
than after OnPaint). Note: Frazier p. 450 to p453 has a discussion on
OnPaint versus PaintEventHandler base. base.OnPaint(e) will trigger
the PaintEventHandler
if (MouseButtons == MouseButtons.Left)
{
Debug.WriteLine("LMB clicked");
}
//shows you can get mouse info from inside OnPaint
if (myGlobalKey == Keys.O)
{
Debug.WriteLine("Oh! O pressed inside paint");
MessageBox.Show("O! from Paint");
} //works fine, but note: since OnPaint refreshes so fast, you’ll
get a dozen or so messageboxes even if you press quickly. Use escape
key to get out of them
if (myGlobalChar == 'w')
{
Debug.WriteLine("small w pressed Paint");
MessageBox.Show("w! from Paint"); //works fine, but you get lots
of messageboxes, as before
}
// another way of triggering keyboard using ModifierKeys
if ((ModifierKeys == Keys.Shift))
{
Debug.WriteLine("Shift pressed");
if (Cursor.Position.X > 50)
{ MessageBox.Show (“shift pressed and cursor > 50”);}
} //shows you can get/use mouse cursor from inside OnPaint
// works fine, but I could not get documentation on how ModifierKeys
is declared, so I prefer to use Keys as above.