How to setup a universal mouse click event WPF C#? - c#

Is it possible to have a universal / global event fire on each mouse click, even if the actual program is minimized, or the click isn't on a window that is part of the application?
Here is code I have tried so far :
// Initialize Global Pre-Process Input
InputManager.Current.PreProcessInput += Current_PreProcessInput;
private void Current_PreProcessInput(object sender, PreProcessInputEventArgs e)
{
if (e.StagingItem.Input is MouseButtonEventArgs)
{
GlobalClickEventHandler(sender, (MouseButtonEventArgs)e.StagingItem.Input);
}
}
private void GlobalClickEventHandler(object sender, MouseButtonEventArgs input)
{
if (input.LeftButton == MouseButtonState.Released)
{
}
}
Problem here is; this only works when a click is in a window part of the application.
Is there some alternative way to have an event fire even if the application is minimized or the click is outside any application window?
Any help or advice will be appreciated.

Related

Binding multiple mediaplayers to multiple keys C# WPF

So, I'm trying to build kind of a sampler. Just for fun. I'm kinda at a loss here;
I've created a grid of 4x4 in wpf and filled them with buttons. Each button has a corresponding mediaplayer (so they would not cut off the other audio when triggered)
I've added a clickevent for each button and a keydown event as wel. Now, the click events all work fine. But the keydown event only works for the last button I clicked on. The rest is unresponsive. Can anyone tell me how to fix this?
a keydown event:
private void Pad_1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.NumPad7)
{
playPad(mediaPlayer1, mp1);
}
}
and my playPad method:
public void playPad(MediaPlayer mediaPlayer, string mp)
{
if (mp != null)
{
mediaPlayer.Open(new Uri(mp));
mediaPlayer.Play();
}
}
Thanks in advance!

Separating NotifyIcon_Click and ItemMenu_Click events

I have an application which runs mostly within the taskbar, and its icon may be left clicked in order to open a Windows form.
The NotifyIcon also has a few MenuItems which can be accessed by right clicking, and each have their own click events to serve their functionality.
private void menuItem4_Click(object Sender, EventArgs e)
{
if (programEnabled == false)
{
programEnabled = true;
Console.WriteLine("Enabled!");
}
else
{
programEnabled = false;
Console.WriteLine("Disabled!");
}
}
//Icon clicked!
private void notifyIcon1_MouseClick(object Sender, EventArgs e)
{
//Reopen the form
Form f = new TemperForm();
}
Every time a MenuItem click event is activated however, NotifyIcon_Click also runs and I need that event for opening the Windows form. I don't want every time I click any MenuItem to also run the code that I wish to run when the icon is clicked.
How can I still use the NotifyIcon_Click event while not having it run every time I click a MenuItem?
I've been at this for hours and it's driving me up the wall!

Show On Screen Key Board on mouse left button down on PasswordBox and should Hide when click outside the box

Platform: C# WPF
Environment: Visual Studio 2013
Question # 1: I want to show third party on screen keyboard on mouse left button down on PasswordBox control of C# WPF. I used the following code:
private void PasswordBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
}
But it does not start on screen key board. Instead it triggers on MouseDoubleClick and GotFocus events.
Question # 2:
I want to Hide on screen keyboard when mouse click outside the PasswordBox and Show again on mouse left button down inside box.
Question # 3:
I want to show keyboard on single click instead of mouse double click
You could handle the PreviewMouseLeftButtonDown event for the parent window. Something like this:
bool isVisible = false;
PreviewMouseLeftButtonDown += (ss, ee) =>
{
if (!passwordBox.IsMouseOver && isVisible)
{
System.Diagnostics.Process.GetProcessesByName("CCOnScreenKeyboard")?.FirstOrDefault()?.Kill();
}
else if (!isVisible)
{
System.Diagnostics.Process.Start("D:\\CCOnScreenKeyboard.exe");
isVisible = true;
}
};
I belive the best way to do this will be through using the Focus events, as you only want the keyboard when you're interacting with the PasswordBox, and for it to go once you have stopped interacting.
private void PasswordBox_GotFocus(object sender, RoutedEventArgs e) =>
Process.Start("D:\\CCOnScreenKeyboard.exe");
private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
{
foreach (var process in Process.GetProcessesByName("CCOnScreenKeyboard"))
process.Kill();
}

How to launch method when window got focus again in WPF?

My app is multi-window, here is quickly how it works:
In main window I have a list of items, when I click on one, it opens another window where I can modify it. When I close that window, I want main window to refresh its content. I've tried many event handlers including GotFocus() but it doesn't want to launch my method to refresh the list. Any advise?
If you want something to happen when the other window is closed, you can subscribe to its closed event. This will fire when the windows is closed.
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window1();
wnd.Closed += wnd_Closed;
wnd.Show();
}
void wnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("Closed");
}

how to handle the Windows Form got focus event in C#?

I have Windows Form.Now i want to perform some function when the form get focused(that was already opened).For example,form has already opened, and user minimized that window and perform some other function.Finally, he will maximize that form to doing function.So, on that time, i want to perform function(while getting focused to the user). how i do it?
You can try the Form's Activated event.
EDIT:
This is the code I tried
public Form1()
{
this.Activated += new EventHandler(Form1_Activated);
}
void Form1_Activated(object sender, EventArgs e)
{
Console.WriteLine("Activated");
}

Categories

Resources