So I have KeyDown event that is not working correctly. The pacman appears on the map but does not respond to any key presses. There are no errors.
Here it is (Gameboard is name of the form, mapPictureBox the name of the picture box):
private void Gameboard_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
pacman.yPosition += 10;
}
else if (e.KeyCode == Keys.Up)
{
pacman.yPosition -= 10;
}
else if (e.KeyCode == Keys.Left)
{
pacman.xPosition -= 10;
}
else if (e.KeyCode == Keys.Right)
{
pacman.xPosition += 10;
}
mapPictureBox.Invalidate();
}
I have an instance of the MovingPacman class declared at the top(where I call images:
MovingPacman pacman = new MovingPacman();
Any ideas?
Set the KeyPreview property of your form to True. By default, edit controls consume key events, this property enable the form to handle events before controls do.
Check this
Related
I've started creating a game and i ran into a problem:
I cant place check boxes because it somehow stops the "players" movement.
I'm moving my "player" using arrow keys.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
left = true;
}
if (e.KeyCode == Keys.Right)
{
right = true;
}
if (e.KeyCode == Keys.Up)
{
up = true;
}
if (e.KeyCode == Keys.Down)
{
down = true;
}
}
And to reset the controls:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
left = false;
}
if (e.KeyCode == Keys.Right)
{
right = false;
}
if (e.KeyCode == Keys.Up)
{
up = false;
}
if (e.KeyCode == Keys.Down)
{
down = false;
}
}
So how can i place a check box and still control my "player"?
The game working without the check box:
https://i.imgur.com/oJmn2uD.gifv
Then i put the check box and not being able to move the "player":
https://i.imgur.com/53WOGjW.gifv
What are other ways to add a checkbox sort of control that i could use for settings tab that would work?
The answer was that i cant use arrows keys because they act as input keys so i replaced them with WASD and it works like a charm!
I have a picturebox that I want to relocate when a key is pressed.
I'm making a game of pong. With the code below I want to make the paddle of player 1, which is a picturebox that should move up and down while a key is pressed. The following code doesn't seem to work:
public partial class Form1 : Form
{
bool upPressed, downPressed;
public Form1()
{
InitializeComponent();
}
private void PongTimer_Tick(object sender, EventArgs e)
{
if(upPressed){
paddlePlayer1.Location = new Point(paddlePlayer1.Location.X, paddlePlayer1.Location.Y + 5);
}
if (downPressed)
{
MessageBox.Show("Numlock");
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = true;
}
if (e.KeyCode == Keys.NumLock)
{
downPressed = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = false;
}
}
}
I used the Numlock to see if the problem was in the picture, this isn't the case. I've put the KeyPreview propery to true but this didnt solve it either. The timer does work properly.
I hope somebody here can tell me what I'm doing wrong.
This is a code example to move a PictureBox:
Drag/drop a PictureBox on a form (in this case Form1).
Change the Background color of the PictureBox to e.g. red. (This way it is visible during runtime).
Implement the KeyDown event of Form1 (in this case Form1_KeyDown).
Run the application.
Start pressing an arrow key and the PictureBox will move.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) { x += 1; }
else if (e.KeyCode == Keys.Left) { x -= 1; }
else if (e.KeyCode == Keys.Up) { y -= 1; }
else if (e.KeyCode == Keys.Down) { y += 1; }
pictureBox1.Location = new Point(x, y);
}
I have this
private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
{
var items = new[] { 500+ objects here };
if (toolStripTextBox1.Text.StartsWith("www."))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (toolStripTextBox1.Text.StartsWith("http://"))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (toolStripTextBox1.Text.StartsWith("https://"))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (items.Any(item => toolStripTextBox1.Text.Contains(item)))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
else
{
webBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl#safe=active&q=" + toolStripTextBox1);
}
}
}
and it litteraly doesn't work. eg. I run it, no errors, all it does is play the windows error sound and won't be functional....
I know the code after the if statement is functional cause i have the exact same code on a button and it works just fine.
Use your code in key up event, not key down, this will make event fully executed and eligible to read the key pressed.
I have adjusted your code and it works, you don't have to use KeyChar just use KeyCode instead and it should work.
private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter || e.KeyValue == (char)Keys.Return)
//if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
{
var items = new[] { 500 + "objects here" };
if (toolStripTextBox1.Text.StartsWith("www."))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (toolStripTextBox1.Text.StartsWith("http://"))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (toolStripTextBox1.Text.StartsWith("https://"))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
if (items.Any(item => toolStripTextBox1.Text.Contains(item)))
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}
else
{
webBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl#safe=active&q=" + toolStripTextBox1);
}
}
Make sure that the control has focus, or redirect the event to the control
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx
Try this for your KeyPress event handler:
private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
{
//Insert code here
It sounds like the problem may be that the method is not bound to the KeyPress event.
In the designer, click on the textbox, and then over to the side where it lists all of the properties, there should be a lightning bolt looking icon, click on that, and then scroll down to KeyPress and make sure it says toolStripTextBox1_KeyPress
EDIT
Alternatively, you can add the event handler programmatically. In your Form_Load event, add the code
toolStripTextBox1.KeyPress += toolStripTextBox1_KeyPress;
and in your Form_Closed event handler, add
toolStripTextBox1.KeyPress -= toolStripTextBox1_KeyPress;
I want a c# split container that ignores cursor keys and can only be controlled with the mouse. How can I do this? This is so I can use the keyboard input in one of the side panels without simultaneously moving the split.
Using e.Handled = true or e.SuppressKeyPress = true to prevent the keys from resizing the splitter didn't work for me.
I was able to do it by setting IsSplitterFixed = true on KeyDown, and IsSplitterFixed = false on MouseDown/MouseMove (to allow resizing by mouse).
e.g.
public Form1()
{
InitializeComponent();
splitContainer1.MouseMove += splitContainer1_MouseMove;
splitContainer1.KeyDown += splitContainer1_KeyDown;
splitContainer1.MouseDown += splitContainer1_MouseDown;
}
void splitContainer1_MouseDown(object sender, MouseEventArgs e)
{
splitContainer1.IsSplitterFixed = false;
}
void splitContainer1_MouseMove(object sender, MouseEventArgs e)
{
splitContainer1.IsSplitterFixed = false;
}
void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
splitContainer1.IsSplitterFixed = true;
}
You may disable the keyboard input handling the KeyDown event of the control and if required, you may handle the event if the input matches specific keys.
Example
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
// if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
// {
e.Handled = true; //Handle the event
// }
}
Moreover, you may stop the splitter from moving by canceling the SplitterMoving event of the SplitContainer control according to the KeyCode gathered from its KeyDown event
Example
Keys KeyCode; //This is the variable we will use to store the KeyCode gathered from the KeyDown event into. Then, check if it matches any of the arrow keys under SplitterMoving event canceling the movement if the result was true
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
splitContainer1.SplitterMoving += new SplitterCancelEventHandler(splitContainer1_SplitterMoving); //Link the SplitterMoving event of splitContainer1 to splitContainer1_SplitterMoving
private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
if (KeyCode == Keys.Up || KeyCode == Keys.Down || KeyCode == Keys.Left || KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
{
KeyCode = Keys.A; //Reset the KeyCode
e.Cancel = true; //Cancel the splitter movement
}
}
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
KeyCode = e.KeyCode; //Set KeyCode to the KeyCode of the event
// e.Handled = true; //Handle the event
}
Thanks,
I hope you find this helpful :)
I have a simple form by which I take input:
12 Buttons, 1 Textbox (disabled & read-only)
this is what I do to handle input
Login_KeyDown() is common method I call for all the KeyDown of every UI component & the form itself..
private void Login_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
{
button3.BackgroundImage = Properties.Resources.button_hover;
button3.ForeColor = Color.White;
pin.Text = pin.Text + "9";
}
else if (e.KeyCode == Keys.Back)
{
button11.BackgroundImage = Properties.Resources.button_hover;
button11.ForeColor = Color.White;
if (pin.Text.Length > 0)
pin.Text = pin.Text.Substring(0, pin.Text.Length - 1);
}
else if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(pin.Text);
}
}
This code works fine when I start the app but after I have clicked on any component, rest of the code works fine but "Enter Key Condition" doesn't work.
My guess is as "Enter Key Condition" is not working for UI components or something like that.
I have also tried using "Key Press Event" which uses KeyPressEventArgs then checking KeyChar == 13 but that is also not working.
What is the problem, and how can I solve it?
p.s.
I have not set any button click events for any button, the app is 100% KBoard based.
Check out PreviewKeyDown. Return raises that event on button controls.
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
MessageBox.Show("I found return");
}
Or alternatively you can force it to raise those special keys in the KeyDown Event by using:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
e.IsInputKey = true;
}
More information: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
Have you tried to use
Keys.Return
Instead
Edit:
Just thought of this. Do you have the acceptbutton set for the main form?
This is because your Form has AcceptButton defined. For example, you have a "OK", "Accept" or "Confirm" button with DialogResult set to "OK". This tells its parent form that there is an AcceptButton, and the Enter event on the form would go to this button.
What you should do is to catch the Enter key at form level. Add this code to the form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
{
//do something
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}