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");
}
}
Related
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?
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 have a DataGridView in C# and need to disable the arrow keys (so they cannot navigate the list via keys). I have tried this:
void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData & Keys.KeyCode)
{
case Keys.Up:
case Keys.Right:
case Keys.Down:
case Keys.Left:
e.Handled = true;
e.SuppressKeyPress = true;
break;
}
}
But it did not disable the arrow keys. Any thoughts?
I tried this handler, but got a compile error:
this.dataGridView1.KeyDown += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_KeyDown);
Error 1 No overload for 'dataGridView1_KeyDown' matches delegate 'System.Windows.Forms.DataGridViewCellEventHandler' Form1.Designer.cs 78 43 FaxMonitorCSharp
Binding the KeyDown event works great so long as the current cell isn't in EditMode, but if it is you'll need to get a bit more creative. I created an inherited DataGridView class, overrode ProcessCmdKey as follows.
class NewDataGridView : System.Windows.Forms.DataGridView
{
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
(FindForm() as Form1).DataGridViewKeyDown((DataGridView)this, keyData);
//return base.ProcessCmdKey(ref msg, keyData);
return true;
}
}
This absorbs all keypresses for the datagridview and redirects them to a method that lives in the container form.
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 want to give a textbox focus when the user starts typing anywhere in my app.
My page inherits from LayoutAwarePage.
Can this be achieved ?
Edit:
I got this code:
// In constructor
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
// Somewhere else in class
void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
this.setSearchboxFocus((int)args.VirtualKey);
}
private void setSearchboxFocus(int keyCode)
{
if (keyCode == 38)
return;
if (keyCode == 40)
return;
if (this.searchBox.FocusState == Windows.UI.Xaml.FocusState.Unfocused)
{
this.searchBox.Text = "";
this.searchBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}
}
For anyone reading this thread in the future, it is because of the webview. I asked a similar question on the msdn forum here. As of Windows 8.1, the webview is implemented as a separate window and completely steals all keyboard input when it has focus without passing any of it up to the controlling application. If you are able to change the HTML in the website being called it may be possible to use javascript listeners to pass events between the application and webview, but I did not test this myself. Unfortunately there does not seem to be any other workaround at this time.
You can handle the KeyDown/KeyUp event for the whole page by subscribing to these events
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp
This might help
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
textBox1.Focus();//sets focus to textBox1 when user presses a key on form
}
How about something like this?
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!textBox1.Focused)
{
textBox1.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
How about, on the Windows.UI.Xaml.Controls.Page:
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
textBox1.Focus(Windows.UI.Xaml.FocusState.Keyboard);
base.OnKeyDown(e);
}