KeyArgs not working - c#

I have this code, which does not want to work! The point is to make a key viewer which shows the pressed cursor keys!
However, none of the keyUpPress method works.
bool debug = true;
void keyUpPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (debug == true)
{
MessageBox.Show("CURSOR UP", "Cursor!");
}
pictureBox1.Image = Properties.Resources.key_up_pressed;
pictureBox1.Refresh();
}

keyUpPress is an event that triggers after lifting a key (after holding it down).
You can't bind a eventHandler to a PictureBox, but you can bind it to the form (if it's a forms application). Example code (with Form1 as the name of the form):
private void Form1_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (debug == true)
{
MessageBox.Show("CURSOR UP", "Cursor!");
}
pictureBox1.Image = Properties.Resources.key_up_pressed;
pictureBox1.Refresh();
}
}

Related

C# KeyDown not working

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);
}

C# - Hide current form and show another one when a key is pressed

How do I make it so that I can hide the current form and show another one when a certain key is pressed? Thats the code I have now
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) { this.Close(); } //press escape to quit
if (e.KeyCode == Keys.Enter) //reload game
{
Are you searching something along the line of this?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) {
this.Close(); // Closing the initial form will close the application
} //press escape to quit
if (e.KeyCode == Keys.Enter) //reload game
{
Form1 f2 = new Form1();
f2.Show();
}
}

Disable delete button on RichTextBox WF

I am trying to disable people from deleting a textbox in a richtextbox. The project is using windows form.
Here is the code I have:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
}
void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)8)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
//The msgbox shows, but the delete still happens within the form.
}
}
Does not show messagebox and does not stop the delete:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown);
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Per the MSDN documentation on KeyPressEventArgs.KeyChar, you cannot get or set the DELETE key using that event. You will need to use the KeyEventArgs.KeyCode instead, subscribing to the KeyDown and KeyUp events.
My solution:
void richTextBox1_TextChanged(object sender, EventArgs e) {
richTextBox1.SelectAll();
richTextBox1.SelectionProtected = true;
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
Side note: yes, this will flicker. Proof of concept only. To avoid the flicker, see How to append text to RichTextBox without scrolling and losing selection?
Instead Of KeyPress event use KeyDown In RichText Box.
try this to prevent from deleting text in RichText Box
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 46)
e.Handled = true;
}
If you want to disallow both delete and backspace You Can Change KeyDown Event as follows
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 8 || e.KeyValue == 46)
e.Handled = true;
}
You must add Back key to prevent delete :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Edit:
Non-selectable RichTextBox :
public class ViewOnlyRichTextBox : System.Windows.Forms.RichTextBox {
// constants for the message sending
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
protected override void WndProc(ref Message m) {
if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
base.WndProc (ref m);
}
}
My solution is some kind of the combination of SerkanOzvatan's and LarsTech's answers. Here is the code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
// Does not show message box...
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
richTextBox1.SelectionProtected = richTextBox1.SelectionLength > 0;
}
It works great :)
And here is another solution of my own which also works great, especially if you want to do with a TextBox (not a RichTextBox), it doesn't have a SelectionProtected, and this is used OK for both TextBox and RichTextBox (just change the class name in the following code accordingly):
public class WritableRichTextBox : RichTextBox
{
protected override bool ProcessKeyMessage(ref Message m)
{
int virtualKey = m.WParam.ToInt32();
if (SelectionLength > 0 || virtualKey == 0x08 || virtualKey == 0x2e)
{
if (virtualKey != 0x25 && virtualKey != 0x26 && virtualKey != 0x27 && virtualKey != 0x28)
return true;
}
return base.ProcessKeyMessage(ref m);
}
}

How do I suppress the noise associated with pressing RETURN in a textbox?

I have read that I can suppress this noise by defining a form accept button, which is something I am trying to avoid (I can point it at a hidden or inactive button I suppose, but since it's not explicitly what I'm trying to do, I'm concerned about side effects)
I use the following snippet to trap the return key and it works just fine, the noise does not occur if I click the button manually.
private void urlTextBox_KeyDown(object sender, KeyEventArgs e) {
if ( e.KeyCode == Keys.Return )
//if ( e.KeyValue.Equals(13) )
{
e.SuppressKeyPress = true;
//e.Handled = true;
goButton.PerformClick();
}
I am targetting .NET 4.0 so I should be able to implement most ideas.
Give this a shot:
private void urlTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
e.SuppressKeyPress = true;
}
}
private void urlTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
goButton.PerformClick();
}
}
Source
It may also work with the KeyDown event but I haven't tested it.

User can press and hold only one keyboard button at a time

I have a standard WinForms-application. I want to implement such functionality:
user can press and hold only one keyboard button at a time. If he tried to press a button, while another button pressed, then it gets no result.
PS: this behavior spreads only to a form that I want, not to all forms of my application.
C#, 2.0 - 3.5, VS 2008
I got something similar than Khadaji
private Keys CurrentKey = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentKey == Keys.None)
{
CurrentKey = e.KeyData;
// TODO: put your key trigger here
}
else
{
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == CurrentKey)
{
// TODO: put you key end trigger here
CurrentKey = Keys.None;
}
else
{
e.SuppressKeyPress = true;
}
}
I banged this out pretty quickly, so you might have to tinker with it to make it work, but it should get you started.
Set your form's KeyPreview to true. Put the in a KeyDown event and KeyUp Event.
Keys MyKey;
bool KeyIsDown = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
e.Handled = true;
}
else
{
MyKey = e.KeyData;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
if (e.KeyData == MyKey)
{
KeyIsDown = false;
}
}
}

Categories

Resources