Trying to program code to Exit application upon ESC in C#? - c#

I am learning more C# programming and have developed an application using Ogre. However the only method I have of exiting the application when running it is, ALT+F4.
I have put in the code a method to Exit with keyPress ESC. However, Visual C# Express is throwing error, and not letting me use ProcessCmdKey. :\
Help?
using System;
using System.Windows.Forms;
protected override bool ProcessCmdKey(ref LogMessageLevel msg, Keys keyData)
{
if (keyData == Keys.Escape) this.Close();
return base.ProcessCmdKey(ref msg, keyData);
}

More standard way is adding close button to the form then set this button as the Form Cancel button.
For more details: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.cancelbutton.aspx
By setting it as Cancel button, it will be triggered automatically upon pressing ESC key.

Try this code :
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
MessageBox.Show("esc pressed!");
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}

Related

Visual Studio - CefSharp and KeyEventArgs doesn't respond to my inputs

Here is the code:
browserControl.KeyDown += new System.Windows.Forms.KeyEventHandler(BrowserControl_KeyDown);
private void BrowserControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.E)
MessageBox.Show("E");
}
I assumed that when i entered the browserControl (a CefSharp ChromiumWebBrowser), it will simply give me a message box with the message "E". Obviously! But no, absolutely nothing i am trying to make something there but i don't think i would download a browser where you get stuck in fullscreen mode because none of the shortcut keys work.
Here is another solution i searched up and appearently should work, and it does if i click a button outside the browser, but not when i need it:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.E)
{
MessageBox.Show("E.. A... GAMES");
}
return base.ProcessCmdKey(ref msg, keyData);
}
I mean come on! I am overriding the whole key process, and still gives me absolutely nothing. What?

Detecting key-press in background in a Winforms project

I am doing a quiz like game (winforms) and I want to introduce the functionality of a cheat or easter egg. If I have a question in front of me and when I press a secret key (ex: Ctrl or other key) a label will show the right answer.
All the questions will show up on the same form, if this helps. The problem is that I can't find a simple and effective way of detecting a key on background.
You can overdide from ProcessCmdKey
Example
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.F1:
break;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Great guys! It is working
let me put the code here for future help:
private void FormName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey)
{
MessageBox.Show("Test");
}
}

TrappedKeyDown doesn't work in my User Control

I would like to retrieve pressed Keys in my UserControl.
The problem is that the keyboard notifications are posted to the control with the focus and this one is my windows form.
I already tried to focus the UserControl, but these approaches are not working.
For example:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) {
this.Visible = false;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
has no effect.
Source: UserControl KeyDown Event Not Fire Against Revoke Of Windows Application KeyDown Event
If I initialize my UserControl the TrappedKeyDown(KeyEventArgs e) Event is not firing.

how to link/set the ENTER to a function in winforms without relation to a Textbox control?

I'm using Winforms.
I've a screen approx. 10 fields. and a Update button.
But I don't want to use neither show on screen a button (btnUpdate).
I just want to show the the fields, they can change some values and by pressing the enter it should execute a function in code behind.
I googled and find some solutions like KeyPress on TextBox or whatever, but I don't want to link this to a TextBox. Then I found form.Acceptbutton = btnUpdate... but then I have to use a button on my designer.
so how can I make a situtation by not USING a Button control to do an update (in other words executing function in code-behind by pressing the Enter Key).
Try overriding the ProcessCmdKey
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Return)
{
//Raise Update Event
return true;
}
else if (keyData == Keys.Escape)
{
//Raise Cancel Event
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

How to get Keypress event in Windows Panel control in C#

i want to get keypress event in windows panel control in c#, is any body help for me...
You should handle the Panel.KeyPress event.
Example
public void MyKeyPressEventHandler(Object sender, KeyPressEventArgs e)
{
... do something when key is pressed.
}
...
(MyPanel as Control).KeyPress += new KeyPressEventHandler(MyKeyPressEventHandler);
The problem is, that at first your main form got the KeyPress and will immediately send this message to the active control. If that doesn't handle this key press it will be bubbled up to the parent control and so on.
To intercept this chain, you have to in your Form.KeyPreview to true and add an handler to Form.KeyPress. Now you can handle the pressed key within your form.
"Panel" objects cannot receive the "KeyPress" event correctly.
I've created Panel overload:
public class PersoPanel : Panel
and used the overridden method ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
to intercept pressed keys:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
MessageBox.Show("You press " + keyData.ToString());
// dO operations here...
return base.ProcessCmdKey(ref msg, keyData);
}
Panel + Keypress - C# Discussion Boards - CodeProject
http://www.codeproject.com/Messages/704386/Panel-plus-Keypress.aspx

Categories

Resources