KeyDown and KeyUp Multiple - c#

I am creating a piano in C Sharp and currently I have keyboard keys to play sounds. For example key A plays Note C. problem I am having is there I want to be pressing multiple keys at the same time and have the sound out. obviously I don't want to have to put all combinations in the keyDown class as I will have to make thousands of if statements. Is there anyway around this?

Windows works with an only one message queue, so in each time only a key down message will be handled at a time unit. What you can do is to get all key down events in a short time interval (0.5 seconds for instace), save all key pressed in a list or a queue, then play all sounds according to the keys asynchronically (using threads). I have never have done this before, but I think should works. Hope helps...
EDIT
Ok, let see:
first the list where to save the keys
List<Key> _keys = new List<Key>();
Then start a timer for checking the keys pressed in a time interval:
var t = new System.Timers.Timer(500); //you may try using an smaller value
t.Elapsed += t_Elapsed;
t.Start();
Then the t_Elapsed method (Note that if you are in WPF an DispatcherTimer should be used, this timer is on System.Timers)
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_keys.Count > 0)
{
//Here get all keys and play the sound using threads
_keys.Clear();
}
}
And then the on key down method:
void OnKeyDownMethod(object sender, KeyPressedEventArgs e) //not sure this is the name of the EventArgs class
{
_keys.Add(e.Key); //need to check
}
You may try this, hope be helpful.

Related

multiple keys keydown on keyboard at once time in c#

I have an application which want to detect multiple keys on Keyboard (keydown event in c#).
In the last, i have limited keys detection (may be 4 keys) on event keydown and also i can't fire the event when "fn" keys keydown.
there are 2 question here:
1. How to detect keys as many as possible
2. How to detect Fn keys when keyboard keydown event.
Here, I us globalKeyboardHook library that i found from others solution.
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.A);
gkh.HookedKeys.Add(Keys.B);
gkh.HookedKeys.Add(Keys.D);
gkh.HookedKeys.Add(Keys.F);
gkh.HookedKeys.Add(Keys.G);
gkh.HookedKeys.Add(Keys.H);
gkh.HookedKeys.Add(Keys.J);
gkh.HookedKeys.Add(Keys.K);
gkh.HookedKeys.Add(Keys.L);
gkh.HookedKeys.Add(Keys.Z);
gkh.HookedKeys.Add(Keys.X);
gkh.HookedKeys.Add(Keys.C);
gkh.HookedKeys.Add(Keys.V);
gkh.HookedKeys.Add(Keys.B);
gkh.HookedKeys.Add(Keys.N);
gkh.HookedKeys.Add(Keys.M);
gkh.HookedKeys.Add(Keys.Q);
gkh.HookedKeys.Add(Keys.W);
gkh.HookedKeys.Add(Keys.E);
gkh.HookedKeys.Add(Keys.R);
gkh.HookedKeys.Add(Keys.T);
gkh.HookedKeys.Add(Keys.Y);
gkh.HookedKeys.Add(Keys.U);
gkh.HookedKeys.Add(Keys.I);
gkh.HookedKeys.Add(Keys.O);
gkh.HookedKeys.Add(Keys.P);
gkh.HookedKeys.Add(Keys.NumLock);
gkh.HookedKeys.Add(Keys.Insert);
gkh.HookedKeys.Add(Keys.FinalMode);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine(e.KeyCode.ToString());
e.Handled = true;
}
I am really pleasure, if you can help me.
thanks.
You can't get a KeyDown event with multiple keys.
However, you can either check for keys that are currently pressed, or you can keep track of all those that are Down at the moment (record them when you get KeyDown and remove when you get KeyUp).
There is a fixed limit to keys being pressed simultaneously based on the hardware keyboard. It may very well be that some key combinations only allow you to register two keys simultaneously, while others are more useable. And of course, there's gaming keyboards that can easily keep track of 20 or more at a time.
There is a managed library MouseKeyHook as nuget which has support for detecting shortcuts, key combinations and sequences. Source code on github. Here is a usage example:
void DoSomething()
{
Console.WriteLine("You pressed UNDO");
}
Hook.AppEvents().OnCombination(new Dictionary<Combination, Action>
{
{Combination.FromString("Control+Z"), DoSomething},
{Combination.FromString("Shift+Alt+Enter"), () => { Console.WriteLine("You Pressed FULL SCREEN"); }}
});
For more information see: Detecting Key Combinations and Seuqnces

How to Console.Beep while (during) mouse click down?

So, I'm trying to do a simple GUI to make music with Beeps on C#. I've been trying but I'm not sure if it's even possible to make a Console.Beep play as I hold down a button for example.
There's the normal Beep() method that just plays a short beep with a medium frequency and there's an overload Beep(int frequency, int duration). What I want to do is actually play it the whole time I'm holding the button, but obviously I can't previously state the duration.
I'm thinking this isn't possible but maybe there is a way?
This is also my first question on the site, so, hey.
You could do it like this, i just tested it and it works, and does not lock up the form while running.
private void Window_MouseDown_1(object sender, MouseButtonEventArgs e)
{
// Starts beep on background thread
Thread beepThread = new Thread(new ThreadStart(PlayBeep));
beepThread.IsBackground = true;
beepThread.Start();
}
private void PlayBeep()
{
// Play 1000 Hz for max amount of time possible
// So as long as you dont hold the mouse down for 2,147,483,647 milliseconds it should work.
Console.Beep(1000, int.MaxValue);
}
private void Window_MouseUp_1(object sender, MouseButtonEventArgs e)
{
//Aborts the beep with a new 1ms beep on mouse up which finishes the task.
Console.Beep(1000, 1);
}

Monitoring keyboard activity in C# while my application is in the background

First of all I need to make it clear that I have no interest in keylogging.
I need a way to monitor keyboard activity at the most basic level while my application is in the background.
I don't need to know which keys, I don't need to save any data, I don't need or plan to hide my application at all, all I need is to know when keys are pressed and invoke a method.
I'm looking for the simplest way to do this possible, I know a reasonable amount of C# but nothing too complex as most of my knowledge is self-taught.
I've looked around for some appropriate ways of doing this and I've found nothing useful. All I've found is a bunch of people saying "No, that's illegal" on forums and source code for in depth keyloggers.
If any of you could advise me on a way to achieve this then I would be most appreciative.
You'll need to use Window Hooks:
Low-Level Keyboard Hook in C#
But beware, Windows security, may be protecting us from doing what you want!
You can monitor keyboard and mouse activity in the background with the Nuget package MouseKeyHook (GitHub).
This code detects when a key is pressed:
private IKeyboardMouseEvents _globalHook;
private void Subscribe()
{
if (_globalHook == null)
{
// Note: for the application hook, use the Hook.AppEvents() instead
_globalHook = Hook.GlobalEvents();
_globalHook.KeyPress += GlobalHookKeyPress;
}
}
private static void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
}
private void Unsubscribe()
{
if (_globalHook != null)
{
_globalHook.KeyPress -= GlobalHookKeyPress;
_globalHook.Dispose();
}
}
You will need to call Subscribe() to start listening, and Unsubscribe() to stop listening. Obviously you need to modify GlobalHookKeyPress() to do useful work.
I needed this functionality in order to write a utility which will turn on the keyboard backlight on a Lenovo Thinkpad when any key is pressed, including CTRL (which KeyPress doesn't catch). For this purpose, I had to monitor for key down instead. The code is the same except we attach to a different event...
_globalHook.KeyDown += GlobalHookOnKeyDown;
and the event handler signature is different:
private static void GlobalHookOnKeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("KeyDown: \t{0}", e.KeyCode);
}
The library can also detect specific key combinations and sequences. For example:
Hook.GlobalEvents().OnCombination(new Dictionary<Combination, Action>
{
{ Combination.TriggeredBy(Keys.A).Control(), () => { Console.WriteLine("You Pressed CTRL+A"); } },
{ Combination.FromString("Shift+Alt+Enter"), () => { Console.WriteLine("You Pressed FULL SCREEN"); } }
});
Microsoft tells you How to: Handle Keyboard Input at the Form Level. As long as you handle the same event(s) this works for any non web application.
You should also take a look at the other questions here on SO, such as Handling Input from a Keyboard Wedge
You could register Windows Hot Key with RegisterHotKey windows API, look at this blog post :
http://www.liensberger.it/web/blog/?p=207

In a C# Console app, the Pause key freezes the display output. Can I disable that?

In a C# Console app, pressing the Pause key freezes the display output. Can I disable that?
I was hoping for a handler like the Console.CancelKeyPress event that handles Ctrl+C input.
Every once in a while a request comes up for hooking keys from a console program. The standard events like CTRL_C_EVENT and CTRL_CLOSE_EVENT do not include the Pause-event. I've tried doing so using a background thread, but I don't seem to manage. However, there's a not-so-hard workaround: use an extra process.
Download this easy-to-use global keyboard hook for C#. Then, when you open that project, take the following code and put it in the Form1.cs:
public partial class Form1 : Form {
globalKeyboardHook globalKeyboardHook = new globalKeyboardHook();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
globalKeyboardHook.HookedKeys.Add(Keys.Pause);
globalKeyboardHook.KeyDown += new KeyEventHandler(globalKeyboardHook_KeyDown);
globalKeyboardHook.KeyUp += new KeyEventHandler(globalKeyboardHook_KeyUp);
}
void globalKeyboardHook_KeyUp(object sender, KeyEventArgs e)
{
// remove this when you want to run invisible
lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
// this prevents the key from bubbling up in other programs
e.Handled = true;
}
void globalKeyboardHook_KeyDown(object sender, KeyEventArgs e)
{
// remove this when you want to run without visible window
lstLog.Items.Add("Down\t" + e.KeyCode.ToString());
// this prevents the key from bubbling up in other programs
e.Handled = true;
}
}
Then, the rest becomes trivial:
Change your program to start the above program and than run normally
Try to type the pause key
It'll be caught by the other program
Your program will NOT be paused.
I tried the above myself and it works.
PS: I don't mean that there isn't a possible way straight from a Console program. There may very well be, I just didn't find it, and the above global keyhook library didn't work from within a Console application.
It's not necessary to attach an hook for this. In your case #PeteVasi, you can modify the console mode to capture Ctrl+C, Ctrl+S, etc... events that are not normally possible to capture.
See my answer to a similar question here.

C# Step by Step Execution

I'm building an app that uses and scanner API and a image to other format converter. I have a method (actually a click event) that do this:
private void ButtonScanAndParse_Click(object sender, EventArgs e)
{
short scan_result = scanner_api.Scan();
if (scan_result == 1)
parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
}
The problem is that the if condition (scan_result == 1) is evaluated inmediatly, so it just don't work.
How can I force the CLR to wait until the API return the convenient result.
NOTE
Just by doing something like:
private void ButtonScanAndParse_Click(object sender, EventArgs e)
{
short scan_result = scanner_api.Scan();
MessageBox.Show("Result = " + scan_result);
if (scan_result == 1)
parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
}
It works and display the results.
Is there a way to do this, how?
Thank you very much!
UPDATE:
Theres an event on the scanner API:
Public Event EndScan() // Occurs when the scanned the image.
But I don't know how to use it. Any Idea?
That really depends on how the API works. If scanner_api.Scan() is blocking, then it will sit at that line waiting for a result. Once it gets the result, the if will evaluate. This can cause your UI to become unresponsive, so you often have to implement some sort of threading to do it in the background. I'm guessing from your question that isn't the way this API works.
Another way this could work is with polling. You check every so often to see what the result is. You don't want to check constantly and use up all your resources (such as CPU), so you check at an interval. Sheldon's answer with a Timer achieves this.
At least one more way this may work is with a callback. You send the API a callback function to call when the status has updated. This can be implemented as events (delegate) you tie into or a regular delegate you pass as a parameter. You'll often see these implemented as "OnStatusChanged", "OnCompleted", etc.
Basically, it's down to what the API supports. Polling usually works, the others have to be supported. Check your API documentation for examples if possible.
You can use a timer (see MSDN: Timer class) that periodically checks whether the scan already completed or not.
You can alternatively use an asynchronous call that calls back when the scanning process is finished. Note that this is the more complicated way.
One way would be with a timer. Set the timer to check every few seconds to check the value in scan_result (which would need to be promoted to a class-level variable for this to work).
So, something like:
public class Scanning
{
private System.Timers.Timer aTimer;
short scan_result;
public Scanning()
{
aTimer = new System.Timers.Timer(1000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}
private void ButtonScanAndParse_Click(object sender, EventArgs e)
{
aTimer.Enabled = true;
scan_result = scanner_api.Scan();
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
if (scan_result == 1)
{
aTimer.Enabled = false;
parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
}
}
}
(This is untested, of course. YMMV.)

Categories

Resources