I'm using axWindowsMediaPlayer playlist, and have some problem - after one video end's - next playing without any pause. I cant pause or stop it even with
private void axWindowsMediaPlayer1_MediaChange(object sender, AxWMPLib._WMPOCXEvents_MediaChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{axWindowsMediaPlayer1.Ctlcontrols.pause(); }}
It doesn't do anything, but must stop the player. What can I do with this? Or even disable AUTOplaying next playlist file, that's would be even better.
Thx a lot for help!
I believe you should be working with the object passed into the method and additionally the PlayStateChange event like so:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)
{
e.Ctlcontrols.pause();
}
}
}
UPDATE: Also I would suggest that you use the axWindowsMediaPlayer1_PlayStateChanged() event rather than Media change as this may be where the issue is occurring.
The '8' is to signify "Media Ended" which if you know how to access it another way then you of course can. If you want to use WMPLib.WMPPlayState.wmppsMediaEnded I'm sure it would yeild the same result.
Related
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);
}
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.
I'm a C# beginner and I have made a little launcher for a game and to start it, I'm using this button event:
private void button5_MouseClick(object sender, MouseEventArgs e)
{
Process.Start("engine.exe", "/load /config debug");
Application.Exit();
}
As you can see this also uses Application.Exit() to close the launcher when the game starts, because they are separate.
What I want to know is if my method is good to start the game + close launcher and also,
I would like to know how to make a pop up message saying "Engine.exe not found" when I'm clicking the button and engine.exe is missing.
Thanks!
try
{
Process.Start("engine.exe", "/load /config debug");
Application.Exit();
}
catch(FileNotFoundException e)
{
MessageBox.Show(e.Message);
}
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
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.)