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?
Related
I'm trying to call function with left and right buttons on keyboard, but not sure how to do it proper way.
In result of this attempt, pressing on left/right keyboard keys just switches between GUI elements usual way, and does not works for given functions. Not sure what is wrong here:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
func1();
}
else if (e.KeyCode == Keys.Left)
{
func2();
}
}
An alternative to enabling keypreview as mentioned in some comments would be to override the ProcessCmdKey Method.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Right)
{
func1();
return true;
}
else if (keyData == Keys.Left)
{
func2();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Please see this MSDN article for more information.
The code you have works correctly, you're just not allowed to press anything beforehand. I think you're looking for a general keydown as shown here
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");
}
}
I made a program in WinForms that shows a blank screen, and then if you press Enter then something happens..
well i used this code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Visible = false;
Colors.Start();
}
Now when I tried to add some buttons to the blank screen, the option to click on Enter just don't work anymore... no matter what I do. and please don't earse this question, I'm kind of new in programming and I know there's alot of questions like that one, but I couldn't understand them...
thanks
Is the form's AcceptButton property assigned to a button?
If so, that could be grabbing the Enter keystroke first.
An example of the suggestion by Hans Passant:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
MessageBox.Show("Enter");
return true; // optionally prevent further action(s) on Enter; such as Button clicks
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Note that if you return true, then controls like the buttons will not get the Enter keystroke.
I have a custom control MyLookUpEdit, it inherites Devexpress LookUpEdit.
In MyLookUpEdit, I have below method:
protected override bool ProcessDialogKey(Keys keyData)
I use this custom control (let's call it nameLookUpEdit) on a form. When I tab through the control, I see ProcessDialogKey is called and it focus to next control according to tabIndex.
Now we hope when enter is pressed, our control can behavior exactly the same as tab pressed. I think I should add in MyLookUpEdit one of those methods:
protected override void OnKeyDown(KeyEventArgs e)
or
protected override void OnEditorKeyDown(KeyEventArgs e)
and capture the enter key and hence call the ProcessDialogKey explicitly:
{
if(e.KeyData == Keys.Enter)
{
ProcessDialogKey(Keys.Tab);
}
base....
}
But when I test on it, I find when nameLookUpEdit is currently focused, I press enter key, neither onKeyDown nor onEditorKeyDown will be called.
Why this happens?
And what is the correctly way to let enter behavior the same as tab?
try overriding ProcesscmdKey ?
edit: Added the return statement for correctness.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
//do stuff
}
return base.ProcessCmdKey(ref msg, keyData);
}
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);
}