here is my code:
MediaPlayer player = new System.Windows.Media.MediaPlayer();
bool playing = false;
bool _bKeyIsDown = false;
protected override void OnKeyDown(KeyEventArgs e)
{
if (_bKeyIsDown) return;
_bKeyIsDown = true;
// play sound;
base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
_bKeyIsDown = false;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (label5.Text == "Waiting 01.wav")
{
MessageBox.Show("No beat loaded");
return;
}
pictureBox6.Image = Form1.Properties.Resources.white_square_button;
try
{
playing = true;
player.Open(new Uri(label37.Text));
player.Volume = (double)trackBar4.Value / 100;
player.Play();
}
catch (FileNotFoundException)
{
MessageBox.Show("File has been moved." + "\n" + "Please relocate it now!");
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
pictureBox6.Image = Form1.Properties.Resources.black_square_button;
player1.Stop();
player1.Close();
playing = false;
}
This what makes is to play a sound while key is down, but the problem is that when you release that key and press another one the sound delays and if you press 2 keys at the same time it only play the first one you pressed.
If you remove if (_bKeyIsDown) return; it does the trick but the sound won't play at its full length.
Is there a way to fix this problem? Thanks!
If its just play and stop 1 sound just do KeyDown event and use bool to toggle:
bool playing = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D1)
{
if (!playing)
{
player.Open(new Uri(yourURI));
player.Play();
playing = true;
}
else
{
player.Stop();
playing = false;
}
}
}
EDIT:
then may i suggest you create a dictionary with all the sounds you need like this:
Dictionary<Keys, Uri> sounds = new Dictionary<Keys, Uri>()
{
{Keys.A,new Uri(#"C:\..\..\youraudiofile.mp3")},
{Keys.B,new Uri(#"C:\..\yourotheraudiofile.wma")}
};
and create a list to keep references to the mediaplayers:
List<MediaPlayer> myplayers = new List<MediaPlayer>();
then only in KeyDown event(remove the onKeyDown,and keyup also):
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
foreach (MediaPlayer m in myplayers)
{
m.Stop();
}
return;
}
if (sounds.ContainsKey(e.KeyCode))
{
MediaPlayer mp = new MediaPlayer();
mp.Open(sounds[e.KeyCode]);
mp.Play();
}
}
Related
I am working on my project and i need to add an action that is only performed while left CTRL is pressed
This is my code:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.Control)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
Now when I run this code and press CTRL all is OK, but when I release CTRL
nothing happened.
I was try this:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
This works fine but I need do this with CTRL
The KeyUp event will fire when you release Ctrl, but the e.Control boolean is not set to True because Ctrl is no longer being held down.
In short: Don't detect the release of Ctrl by inspecting e.Control, inspect the e.KeyCode instead; it will be Keys.ControlKey
if(e.KeyCode == Keys.ControlKey)
{
...
}
On your second code, replace Keys.A with Keys.ControlKey like this:
private void Izrada_kartice_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender,
KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
I have this Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Left)
{
left = true;
right = false;
down = false;
}
}
private void timerMoving_Tick(object sender, EventArgs e)
{
if(Ghost.Bounds.IntersectsWith(panel3.Bounds))
{
timerMoving.Stop();
}
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
}
In the above code as my Ghost(which is moving down) hits the panel3 boundary it stops.
I want my timer to start again as i press Left,Right or up key and only stop when it's moving down,hitting panel3. I tried doing this:
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
but nothing happens, Why?
The simple (though not the optimal) way to achieve this goal pertinent to your task description is by adding the line shown in the following code snippet:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
{
// add this line
if(!timerMoving.Enabled) timerMoving.Start();
left = true;
right = false;
down = false;
up = false;
}
}
and removing this line as shown below:
if (left)
{
Ghost.Left -= 5;
//timerMoving.Start(); - comment off
}
Hope this may help.
I have a text box which the user shouldn't be allowed to write spaces in.
So far I have this code:
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
}
}
It works, but the cursor is being placed in front of the text. Is there another way to do this?
Save the SelectionStart in your OnKeyDown event and then set it again within the OnKeyUpEvent
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
_selectionStart = SearchCriteria.SelectionStart;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
SearchCriteria.SelectionStart = _selectionStart;
NeedsToDelete = false;
}
}
try this :
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
SearchCriteria.Select(SearchCriteria.Text.Length, 0);
}
}
I have no idea how do this.
I know only how do detect one key:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
MessageBox.Show("C key");
}
}
You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".
put a break point in your key down event and press your two keys together. examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}
like I have done for shift and r pressed together
As you can see, you can use a timer event with booleans to detect if two keys are pressed:
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;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyleft && keyup)
{
Console.Beep(234, 589);
}
}
Use this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
string keysPressed = keyData.ToString();
// your code goes here
}
This is what I get for Up + Shift: "Up, Shift"
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;
}
}
}