I have been trying to make a button in C# move based on the user pressing various arrow keys under a Form1 KeyDown method, but i cannot figure out how to make the value of e equal to the users input.
So far i have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
button1.Location = new Point(button1.Top, button1.Left + 1);
}
else if (e.KeyCode == Keys.Right)
{
button1.Location = new Point(button1.Top, button1.Right + 1);
}
else if (e.KeyCode == Keys.Up)
{
button1.Location = new Point(button1.Top +1, button1.Right);
}
else if (e.KeyCode == Keys.Down)
{
button1.Location = new Point(button1.Top-1, button1.Right);
}
}
and then to call it I use
Form1_KeyDown(sender: Keys.A ,e: KeyDown);
but i have no idea what to put in for the value of either sender or e. If it helps, my goal is to be able to have the user maneuver a button around the screen at a set speed. Thanks!
Related
I'm making a mini game. I want to rotate my player with this code when I turn left and right picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX) My moving code is:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
moveRight = true;
}
}
Also, my timer's program is:
private void tmrMovementPlayer_Tick(object sender, EventArgs e)
{
//whenever right arrow is pressed,
if (moveRight == true)
{
//decrease the x variable by 5 (moves right)
x = x + PLAYER_SPEED;
//check for boundaries (if the player is out of the screen)
if (x >= this.ClientSize.Width - picPlayer.Width)
{
//if yes, set it back to the boundary.
x = this.ClientSize.Width - picPlayer.Width;
}
//check the subprogram for info
MovePlayer();
}
}
What should I do at this point? Thanks.
So thanks to the other answer, I found the solution. It was a little bit different. I created another boolean called "goingRight" just for the rotation.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
//make move right true
moveRight = true;
//if i was going left,
if (goingRight == false)
{
//say it im going right
goingRight = true;
//and flip it (only if i was going left before)
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
}
Again, thanks for all the help.
Try something like...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
if (!moveRight)
{
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
moveRight = true;
}
}
}
Im a beginner in C# and im trying to create a windows form application that save a copied text to a textbox when you execute a command with the keyboard. I know there is mouch more to do but where do i start? I suceed to make someting happening with the code below a start at least..
And another question.. is it possible to create more than 2 commands. It doesn't work if i add for example : " && KeyCode.ToString() == "B") "
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "C")
{
MessageBox.Show("CTRL+C");
}
}
Cheers
You would manage this by calling keyDown/Up events. Keep track of each event and which key went down. Then using the Clipboard.GetText() function to copy/paste the text from clipboard into your textbox once both keys are down.
Example,
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
if (keyleft && keyup)
{
textboxOne.Text = Clipboard.GetText(TextDataFormat.Html);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
Used both these as my resources.
Resource One: Detect when two keys are pressed at the same time
Resource Two: http://msdn.microsoft.com/en-us/library/c2thcsx4%28v=vs.110%29.aspx
I am working on a gaming project with a base to help get me started. The base is overall extremely helpful, however there is a part of the code that confuses me as to how to manipulate correctly.
When this code is run, the player sprite will indefinitely move in a direction until the other key press is used. If a playerMoveY is added, it will get stuck moving in diagonals.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
playerMoveX = -1;
}
else if (e.KeyCode == Keys.Right)
{
playerMoveX = 1;
}
Without seeing your full code, it's going to be hard to say for sure, but it is probably because you don't have a KeyUp event to reset the move value.
Similarly to how you created a KeyDown event to use the Form1_KeyDown method, try something like this for KeyUp
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
playerMoveX = 0;
}
else if (e.KeyCode == Keys.Right)
{
playerMoveX = 0;
}
}
I'm trying to make it so that when I press the up arrow, it moves the picture box up, the down arrow moves down, ect. But I can't seem to get it to work. It is giving me the error:
Can't modify the return value of
'System.Windows.Forms.Control.Location' because it is not a variable
This is my code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up)
{
ImgGuy.Location.Y--;
}
else if (e.KeyCode == Keys.Down)
{
ImgGuy.Location.Y++;
}
else if (e.KeyCode == Keys.Left)
{
ImgGuy.Location.X--;
}
else if (e.KeyCode == Keys.Right)
{
ImgGuy.Location.X++;
}
Any help is greatly appreciated.
You have to recreate new Location:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Point l;
if(e.KeyCode == Keys.Up)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y - 1);
}
else if (e.KeyCode == Keys.Down)
{
l = new Point(ImgGuy.Location.X, ImgGuy.Location.Y + 1);
}
else if (e.KeyCode == Keys.Left)
{
l = new Point(ImgGuy.Location.X - 1, ImgGuy.Location.Y);
}
else if (e.KeyCode == Keys.Right)
{
l = new Point(ImgGuy.Location.X + 1, ImgGuy.Location.Y);
}
ImgGuy.Location = l;
}
Try this:
ImgGuy.Location = new Point(ImgGuy.Location.X+1, ImgGuy.Location.Y+1) // etc
The problem is that Location returns a copy of the location.
Alternatively, set Control.Left and Control.Top instead.
You need to produce a new point
In this case X is increased aka move left
Pic.Location = new Point(Pic.Location.X + 1, Pic.Location.Y);
How can I determine in KeyDown that ⇧ + Tab was pressed.
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift)
{
//do stuff
}
}
can't work, because never both keys are pressed exactly in the same second. You always to at first the Shift and then the other one..
It can't work, because never both keys are pressed exactly in the same second.
You're right that your code doesn't work, but your reason is wrong. The problem is that the Tab key has a special meaning - it causes the focus to change. Your event handler is not called.
If you use a different key instead of Tab, then your code will work fine.
If you really want to change the behaviour of Shift + Tab for one specific control, it can be done by overriding ProcessCmdKey but remember that many users use the Tab key to navigate around the form and changing the behaviour of this key may annoy those users.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (DateTimePicker_BirthDate.Focused && keyData == (Keys.Tab | Keys.Shift))
{
MessageBox.Show("shift + tab pressed");
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
If you are looking for a key press combination (Tab, then Shift) like Ctrl K + D you will have to use this modified example which was taken from MSDN social.
private StringBuilder _pressedKeys = new StringBuilder();
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
_pressedKeys.Append("Tab");
return;
}
if (e.Modifiers == Keys.Shift)
{
_pressedKeys.Append("Shift");
return;
}
if (_pressedKeys.ToString()."TabShift")
{
MessageBox.Show("It works!");
_pressedKeys.Clear();
}
else
{
_pressedKeys.Clear();
}
base.OnKeyDown(e);
}
First hook the Tab keypress event, then during the event, check the state of the Shift key. Keep in mind that there are two shift keys; make sure you check both of them.
This very related post shows how to check the state of modifier keys:
How to detect the currently pressed key?
Edit: an insight provided by another answerer who justly deserves an upvote is that the default behavior of the tab key (to change control focus) must be suppressed.
You can find your answer in
this post
It's Simple.
You can do that using KeyUp Event in the TextBox
private void txtBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
Or
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
It's Simple.
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}