I am fairly new to c# and I want to make a picture box move when I press the WASD keys, but the picture box refuses to move. The picture box is not docked, locked or anchored. This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Imagebox_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) x -= 1;
else if (e.KeyCode == Keys.S) x += 1;
pictureBox1.Location = new Point(x, y);
}
}
}
I have no idea what's going on! thanks for the help!
There are 2 issues with your code:
Set the form's KeyPreview property to true otherwise it the PictureBox will get the KeyDown event. This was preventing Form1_KeyDown from being called.
This code block has a subtle bug:
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) x -= 1;
else if (e.KeyCode == Keys.S) x += 1;
If you look closely, you're only modifying the x coordinate.
All together:
public Form1()
{
InitializeComponent();
// Set these 2 properties in the designer, not here.
this.KeyPreview = true;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) y -= 1;
else if (e.KeyCode == Keys.S) y += 1;
pictureBox1.Location = new Point(x, y);
}
You need to set the form's KyePreview property to true otherwise form will not take that key down event.
Secondly you are only changing x value and not y value.
The whole completed code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Imagebox_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) y -= 1;
else if (e.KeyCode == Keys.S) y += 1;
pictureBox1.Location = new Point(x, y);
}
}
}
Related
I am new to C# and wanted to know how I could make it so that once the user presses the enter key, the current location of the image becomes its fixed location. I was thinking that the best way to do it would be using a while loop. Help would really be appreciated. The following is my code for moving my image:
private void pictureBox1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
In this solution i have used a global bool object and changed flag to true once an enter key is pressed.
I have a form with Picture Box and on the form_KeyDown event i placed your code with small change.
bool bIsEnterKeyPressed = false;
private void frmSampleJson_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
bIsEnterKeyPressed = true;
}
if (!bIsEnterKeyPressed)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
{
if (e.KeyCode == Keys.Right) x += 50;
else if (e.KeyCode == Keys.Left) x -= 50;
else if (e.KeyCode == Keys.Up) y -= 50;
else if (e.KeyCode == Keys.Down) y += 50;
pictureBox1.Location = new Point(x, y);
}
}
}
Once enter is pressed the bIsEnterKeyPressed is changed to true and the position will not be changed after that.
I wish the image in picture box could flip when key left is pressed, as the code shown below, but it didn't flip when key left is pressed. Anyone could help ? Thanks a lot!
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right)
{
x += 10;
}
else if (e.KeyCode == Keys.Left)
{
pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
pictureBox1.Refresh();
x -= 10;
}
else if (e.KeyCode == Keys.Up)
{
y -= 10;
}
pictureBox1.Location = new Point (x, y);
pictureBox1.Invalidate();
}
}
}
Instead of Image you are trying rotating the picture box. As per the example given in the link. rotate the image then assign it to picture box.
Refer this link officially from Microsoft docs https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx
So I have a picturebox named picbox1 and an image inside it. I need it to move up if W is pressed , left if A is pressed and you get the idea. What's the easiest way to do this? Could you please explain the code as well.
Here is my code(just in case):
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
public class MyProgram {
public static void Main() {
Form form1 = new Form();
form1.Show();
form1.Width = 800;
form1.Height = 600;
PictureBox picbox1 = new PictureBox();
form1.Controls.Add(picbox1);
picbox1.Image = Image.FromFile("c:\\Users\\FakeUsername\\Downloads\\AnimatedTest.gif");
Application.Run();
}
}
Creating form and picture box dynamically in Program.cs code is not good idea. You should declare your Form1 from in another file. Put pictureThen use KeyPress event:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
int offset = 10;
if(e.KeyChar == 'a')
{
pictureBox1.Location = new Point(pictureBox1.Location.X - offset, pictureBox1.Location.Y);
}
else if(e.KeyChar == 'w')
{
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - offset);
}
else if (e.KeyChar == 's')
{
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + offset);
}
else if (e.KeyChar == 'd')
{
pictureBox1.Location = new Point(pictureBox1.Location.X + offset, pictureBox1.Location.Y);
}
}
I'm using global keyboard hooks as a keyboard listener when the window is out of focus. I'm half way through some code and I realise that for whatever reason, putting caps lock on changes the output of KeyEventArgs. Using shift/no shift outputs ordinary results but if I were to put caps lock on, A will become U, B will become V, C will become W and so on. Here's a look at the code that does this (will add global keyboard hooks if necessary):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
namespace WindowsFormsApplication1
{
public partial class Form6 : Form
{
globalKeyboardHook gkh = new globalKeyboardHook();
int sign_Indicator = 0;
double variable1;
double variable2;
int addBit = 0;
int subBit = 0;
int multBit = 0;
int divBit = 0;
int modBit = 0;
Boolean fl = false;
String s, x;
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.A);
gkh.HookedKeys.Add(Keys.B);
gkh.HookedKeys.Add(Keys.C);
gkh.HookedKeys.Add(Keys.D);
gkh.HookedKeys.Add(Keys.E);
gkh.HookedKeys.Add(Keys.F);
gkh.HookedKeys.Add(Keys.G);
gkh.HookedKeys.Add(Keys.H);
gkh.HookedKeys.Add(Keys.I);
gkh.HookedKeys.Add(Keys.J);
gkh.HookedKeys.Add(Keys.K);
gkh.HookedKeys.Add(Keys.L);
gkh.HookedKeys.Add(Keys.M);
gkh.HookedKeys.Add(Keys.N);
gkh.HookedKeys.Add(Keys.O);
gkh.HookedKeys.Add(Keys.P);
gkh.HookedKeys.Add(Keys.Q);
gkh.HookedKeys.Add(Keys.R);
gkh.HookedKeys.Add(Keys.S);
gkh.HookedKeys.Add(Keys.T);
gkh.HookedKeys.Add(Keys.U);
gkh.HookedKeys.Add(Keys.V);
gkh.HookedKeys.Add(Keys.W);
gkh.HookedKeys.Add(Keys.X);
gkh.HookedKeys.Add(Keys.Y);
gkh.HookedKeys.Add(Keys.Z);
gkh.HookedKeys.Add(Keys.D1);
gkh.HookedKeys.Add(Keys.D2);
gkh.HookedKeys.Add(Keys.D3);
gkh.HookedKeys.Add(Keys.D4);
gkh.HookedKeys.Add(Keys.D5);
gkh.HookedKeys.Add(Keys.D6);
gkh.HookedKeys.Add(Keys.D7);
gkh.HookedKeys.Add(Keys.D8);
gkh.HookedKeys.Add(Keys.D9);
gkh.HookedKeys.Add(Keys.D0);
gkh.HookedKeys.Add(Keys.Return);
gkh.HookedKeys.Add(Keys.Shift);
gkh.HookedKeys.Add(Keys.CapsLock);
gkh.HookedKeys.Add(Keys.Back);
gkh.HookedKeys.Add(Keys.Escape);
gkh.HookedKeys.Add(Keys.Space);
gkh.HookedKeys.Add(Keys.OemPeriod);
gkh.HookedKeys.Add(Keys.Oemcomma);
gkh.HookedKeys.Add(Keys.OemMinus);
gkh.HookedKeys.Add(Keys.Oemplus);
gkh.HookedKeys.Add(Keys.OemPipe);
gkh.HookedKeys.Add(Keys.OemBackslash);
gkh.HookedKeys.Add(Keys.OemOpenBrackets);
gkh.HookedKeys.Add(Keys.OemCloseBrackets);
gkh.HookedKeys.Add(Keys.OemQuotes);
gkh.HookedKeys.Add(Keys.OemSemicolon);
gkh.HookedKeys.Add(Keys.Oemtilde);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
}
public static string location = "C:\\Users\\Hayden\\Desktop\\log.txt";
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Shift)
{
if (e.KeyCode == Keys.OemPipe)
{
System.IO.File.AppendAllText(#location, "|");
}
else
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString());
}
}
else
{
if (e.KeyCode == Keys.Return)
{
System.IO.File.AppendAllText(#location, "[RETURN]");
}
else if (e.KeyCode == Keys.Back)
{
System.IO.File.AppendAllText(#location, "[BACKSPACE]");
}
else if (e.KeyCode == Keys.Space)
{
System.IO.File.AppendAllText(#location, " ");
}
else if (e.KeyCode == Keys.OemPeriod)
{
System.IO.File.AppendAllText(#location, ".");
}
else if (e.KeyCode == Keys.Oemcomma)
{
System.IO.File.AppendAllText(#location, ",");
}
else if (e.KeyCode == Keys.OemPipe)
{
System.IO.File.AppendAllText(#location, "\\");
}
else if (e.KeyCode == Keys.CapsLock)
{
System.IO.File.AppendAllText(#location, "");
}
else if (Control.IsKeyLocked(Keys.CapsLock))
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString());
}
else
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString().ToLower());
}
}
}
How can I fix this? I have no idea why it's changing the letter completely just because I put caps lock on. Shift works fine though.
The globalKeyboardHook class you link to has a AddModifiers(Keys key) method with this line:
if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) key = key | Keys.CapsLock;
This code checks if Cap Lock is on and if so it ORs the key variable which is later returned by the method.
The problem is that Caps Lock is not a modifier key and shouldn't be manipulated this way. This is the reason you're seeing the strange key values you are when Caps Lock in on.
Incidentally, only Control, Shift and Alt are modifier keys and AddModifiers() handles these.
If you remove this line I believe you'll be fine.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Game2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void Move_Ship(int X_Coord ,int Y_Coord, string Positioning)
{
if(Positioning == "Rechts")
{
X_Coord += 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Links")
{
X_Coord -= 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Up")
{
Y_Coord -= 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Down")
{
Y_Coord += 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Control.ModifierKeys == Keys.Up)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Up");
};
if (Control.ModifierKeys == Keys.Down)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Down");
};
if (Control.ModifierKeys == Keys.Left)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Links");
};
if (Control.ModifierKeys == Keys.Right)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Rechts");
};
}
}
}
If I did this right my piture should be moving the but it doesn't do anything at all.
Did I do something wrong and if so please tell me?
One possible problem is usage of Control.ModifierKeys
Gets a value indicating which of the modifier keys (SHIFT, CTRL, and ALT) is in a pressed state.
If you want to use direction keys - listen for keyDown event and save direction there. Sample and details Control.KeyDown:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode == Keys.Down)
{
direction = "Down";
}