I am building a robot car with a Raspberry Pi 3, using C#. I am able to control it with either a button on the user interface or with a keyboard.
The problem with the buttons is that when I click a button, the function won't stop until I press another button. I either need a way of checking while the button is being clicked or one what checks when the button is unclicked.
I have looked through other posts on button holding, but can't really get anything to work.
In the case of key presses, it works correctly because it stops moving when I stop pressing the button. I just add this to the in the XAML page. This is due to the KeyUp bit.
KeyDown="Background_KeyDown_1" KeyUp="Background_KeyUp_1">
This is the C# code.
//Event for when key is pressed
private void Background_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
KeyDirection(e.Key);
}
//Event when key is unpressed, emulates 'Enter' which represents stop.
private void Background_KeyUp_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
KeyDirection(Windows.System.VirtualKey.Enter);
}
//This guy is called when a key is pressed or unpressed.
public void KeyDirection(Windows.System.VirtualKey vkey)
{
//Detects which key has been pressed, then calls Direction();
switch (vkey)
{
case Windows.System.VirtualKey.W:
Direction("forwards");
break;
case Windows.System.VirtualKey.A:
Direction("left");
break;
case Windows.System.VirtualKey.S:
Direction("backwards");
break;
case Windows.System.VirtualKey.D:
Direction("right");
break;
case Windows.System.VirtualKey.Enter:
Direction("stop");
break;
default:
Direction("stop");
break;
}
}
As of current, I am unable to find a way of getting this to work. I was hoping someone who has got it to work previously could show me how they did it.
Cheers, Callum :)
Related
Basically, I've named my mousedown event to be LBTNDOWN, and I've linked the event together with 3 other buttons. I want to make a switch case for each button when it's pressed down, it does something. And I'll also be making a separate mouseup event that does something when the mouse is released, but I'm already stuck at mousedown.
I've tried almost everything and researched so many solutions however it doesn't work! I'm desperate as I have to submit this project tomorrow omg!
private void LBTNDOWN(object sender, MouseEventArgs e)
{
///Code
switch (e.Button)
{
case btnCFL:
txtbox1.text = '1';
break;
case btnCFR:
txtbox1.text = '2';
break;
}
}
I expected the output to be for example when button CFL is pressed down, textbox1 will change to 1, then when button CFR is pressed down, textbox1 will change to 2.
I don't think "switch (e.Button)" is well supported.
Please try below code:
private void LBTNDOWN(object sender, MouseEventArgs e){
///Code
switch ((sender as Button).Text){
case "CFL":
txtbox1.text = '1';
break;
case "CFR":
txtbox1.text = '2';
break;
default:
Console.WriteLine("Default case should be included as a good habit");
break;
}
}
If text cannot distinguish these buttons, you may use tag property of button instead.
So I'm trying to make a simple calculator. The user can only input the numbers by the buttons on the form or by the numpad. This is the code I have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = "";
switch (e.KeyCode)
{
case (Keys.NumPad1):
key = "1";
break;
case (Keys.NumPad2):
key = "2";
break;
default:
break;
}
txt_string.Text = txt_string.Text + key;
}
If I make a breakpoint on the KeyDown function and press the Numpad keys (and every other keys) the program doesnt even comes to that breakpoint.
Do I have to change something on my Form to detect the Keys?
You'll need to set KeyPreview to true (property on the form). Also, I would advise against trying to debug the behaviour - because you may affect the behaviour you're testing (Debug.WriteLine()) is your friend here.
Just to point out that many keyboard doesnt have numpad. You can check if the key is a integer.
void Form1_KeyDown(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
txt_string.Text += e.KeyChar;
}
}
This is more a Code Review than a solution though.
well i'm new to c# and i'm using Visual studio 2012.
i'm trying to make a checkbox with the appearance of a button.
when a keyboard key is pressed i would like for it to show the same way when the mouse clicks a button. If i hit the A key the button/checkbox is pressed down and if A key is hit again the button/checkbox is raised up.
i got this to work with just the button1 but i can't get it to show the pressing of the button by using this code
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on button1
ShowPictureButton.PerformClick();
break;
default:
break;
}
i figured i can use a checkbox so it will stay down when pressed.
If you are saying that you are using a Checkbox with it's Appearance Property set to Button you could do something like this
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on CheckBox's
ShowPictureButton.Checked = !ShowPictureButton.Checked;
break;
default:
break;
}
first set KeyPreview in your form properties to true
add events in your form (keypress and mouseclick) and then write your code, like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
checkBox2.Checked = !checkBox2.Checked;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Visible = !checkBox1.Visible;
}
My form has several buttons such as "Scan" and "Exit". I have seen in many programs where buttons will be useable by keypress. Lots of times the unique key to press is underlined in the text on the button (I don't know how to use an underline option on these forums!). I went to the form and added a keypress event:
private void Form1_KeyPress(object sender, KeyPressEventArgs key)
{
switch (key.ToString())
{
case "s":
Run_Scan();
break;
case "e":
Application.Exit();
break;
default:
MessageBox.Show("I'll only accept 's' or 'e'.");
break;
}
}
But then pressing 's' or 'e' on the form doesn't do anything. Not sure where I'm going wrong here?
Overriding ProcessKeyCommand will accept input from anywhere on the form. You should add a modifier however, since pressing 's' or 'e' in a textbox, for example, will also trigger the action.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
switch (keyData)
{
case Keys.S:
Run_Scan();
break;
case Keys.E:
Application.Exit();
break;
default:
MessageBox.Show("I'll only accept 's' or 'e'.");
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
I think you are looking for what are called Access Keys: There are defined by the '&' symbol.
Get rid of your KeyPress event handler. You will not need it.
Change the text of your buttons to "&Scan" and "&Exit".
Also: Here are some guidelines about using access keys in windows applications.
key.ToString() is the wrong method to call. You want to access the key property: key.KeyChar.
See the MSDN here for more info on the KeyPressEventArgs, which includes examples.
You can put an ampersand in front of the letter you want to make a hot key for the button in the Text property of your button. You can set the Text property from the Properties pane in the form designer, or you can set it programmatically. Below is an example of programmatic approach.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// make the 'B' key the hot key to trigger the key press event of button1
button1.Text = "&Button";
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("B");
}
}
I want to write a simple text to speech program.
First, I want to make the program play only the written symbol. For example, if I type 'a' I want the program to say 'a' (I have recorded all of them), so when I type a word, it should spell it.
However, I am a beginner in C# and .Net and don't how to make the program understand the text I type. For example, in java I heard that there is a keyListener class, but I don't know which class should I use. I looked on MSDN but couldn't find it.
Which class or function should I use to listen to typed keys?
I suppose you are planning to use Windows Forms to achieve this.
The solution would be pretty simple. These events include MouseDown, MouseUp, MouseMove, MouseEnter, MouseLeave, MouseHover, KeyPress, KeyDown, and KeyUp. Each control has these events exposed. You just need to subscribe to it.
Please refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
There would be a little bit of logic to find whether a complete word has been typed or not. A simple soultion would be , when space has been pressed, you can assume a word has been completed. Its very crude logic, as the user may have typed in wrong spelling and want hit backspace and correct the spelling. You may want to add lag to it.
If you are using Visual Studio like every other C# developer here is a more detailed code example:
Create a Windows Form and go to the [Design].
Select its properties (RMB=>properties), navigate to Events and double click LMB on KeyDown
VS will create and bind the event for you
Handle the KeyEventArgs depending on its value.
Example:
private void NewDialog_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.A:
{
MethodToOutputSound(AEnum);
break;
}
case Keys.B:
{
MethodToOutputSound(BEnum);
break;
}
case Keys.F11:
{
DifferentMethod();
break;
}
case Keys.Escape:
{
this.Close();
break;
}
default:
{
break;
}
}
}
Or use a lot of ifs
private void NewDialog_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.A)
{
MethodToOutputSound(AEnum);
}
if(e.KeyData == Keys.B)
{
MethodToOutputSound(BEnum);
}
...
}
Create a Windows Form with a TextBox in it. Handle the KeyPress event - that will give you the actual character that the user types. KeyDown and KeyUp won't help you.
You need to check the KeyChar property. Like this:
void MyEventHandler(object sender, KeyPressEventArgs e) {
// Do stuff depending on the value of e.KeyChar
}
private void button1_Click_1(object sender, EventArgs e)
{
string word = textBox1.Text;
foreach (char i in word)
{
switch (i)
{
case 'a':
case 'A': { // play sound a
break;
}
default:
{
// play no sound
break;
}
}
}
}