App not getting keyboard focus when entering from other app - c#

In WPF I have a Window_KeyDown event which changes the mouse cursor shape when the user presses shift. It works as expected except if I have clicked on another app. So I added a Window_MouseEnter event like this to grab keyboard focus when the mouse reenters my app:
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
IInputElement b = Keyboard.Focus(this);
this.Focus();
Debug.WriteLine(b + DateTime.Now.ToLongTimeString());
}
I can see the MouseEnter event firing (with the debug line) when the mouse enters the app but my app still doesn't get keyboard events until I click in the app.
It's irritating because the mouse cursor changes properly when the mouse enters controls within my app so one would expect things to work but the shift-mouse functions don't work until after a click.
What am I missing?

I found that the secret is the Activate() method. I put it in the MouseEnter function which has a side-effect of forcing the entire app to show if some of it was hidden by other apps.
With Activate(), the Focus() method is not needed.

Related

Activate Click and MouseDown events at the same time?

I am trying to create a form where the user selects one of 2 radio buttons, "fast" and "slow", then presses a "go" button on the form. It should work as follows:
When "fast" is selected and "go" is pressed, the user needs to continue holding down the button in order to make the player move and the player stops when the user releases their finger from the button. For this, I am using the MouseDown and MouseUp events.
If "slow" is selected, the user can only move one step at a time, regardless of how long the button is held down. for this, I am using the Click event.
To test this concept, I put a MessageBox in the Click and MouseDown events to see how they work together. When I click on the button, however, I am only seeing the Mousedown event get triggered, regardless of which radio button is selected and the Click event is only triggered when the go button is in focus and I press enter.
How can I make it so that both events get called simultaneously (and then I can put the respective if statement in to differentiate between "fast" and "slow" radio buttons)? (The button is wired up to these events by double-clicking on their respective handler in the design window, not hardcoded).
private void go_button_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mousedown"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("Mosuedown");
}
private void go_button_Click(object sender, EventArgs e)
{
Console.Out.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
Console.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("go_buttonclick");
}
I found what I was doing wrong, both events are being triggered at the same time, but the MessageBox in the MouseDown event wasn't allowing me to see the Click and MouseUp events also being triggered. When I turned off the MouseDown MessageBox, the other 2 MessageBoxes appeared, thanks!

Control.Capture is always true?

I have created a Windows Forms application, but I get strange behaviour from the Capture property of a control.
On a blank form, with a single label called "label1" and the code
public Form1()
{
InitializeComponent();
label1.MouseDown += pictureBox1_MouseDown;
}
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Debug.WriteLine(label1.Capture);
label1.Capture = !label1.Capture;
Debug.WriteLine(label1.Capture);
}
I observed, that the first WriteLine always says "True", the second one always says "False" when the button is clicked. Even when clicked multiple times.
The label never reacts to clicks outside its border, not even when I use label1.Capture = true instead.
Am I misunderstanding the expected behaviour of the "Capture" property? I expected the initial value to be false, and the label not to react to clicks outside, after the first click I expect the value to be true, and the label to react to all mouse clicks, even outside its borders.
In a MouseDown event, the Capture for the control always set to true initially. So normally if you perform a MouseDown and then without releasing mouse button move your mouse out of the control and then release mouse button, the MouseUp event of the control will be fired.
If you set Capture to false in MouseDown, then the mouse up event of your control will only fire if your mouse is over the control and if you move the mouse out of your control and then release mouse button, the MouseUp event of your control will not raise.
Also in MouseUp the capture will be released automatically.
For more information take a look at these resources:
WmMouseDown method source code
WmMouseUp method source code
CaptureInternal proprty source code
SetCapture documentations
ReleaseCapture documentations

ListView click event not triggered when clicking on empty areas

I have a ListView control on my form. I have set its display mode to LargeImageList. I need to handle the items inside this control. So I have written code for its click event. But I see now that this event is not triggered when I click in an empty area inside it.
How can I make my ListView aware of the clicks on its area regardless.
To capture mouse clicks on the "white space" around the ListView items, you will need to use the MouseDown/MouseUp events.
This will also capture clicks to the items as well.
I've used the Global Mouse Hook for similar issues. You can use it to detect Mouse Clicks anywhere on the screen, then just check the click was within the listview control bounds.
Grab the code from Global Mouse Key Hook
IKeyboardMouseEvents m_GolbalHook = Hook.GlobalEvents();
m_GolbalHook.MouseClick += m_GolbalHook_MouseClick;
private void m_GolbalHook_MouseClick(object sender, MouseEventArgs e)
{
if (listView.Bounds.Contains(e.Location)) && (e.Button == System.Windows.Forms.MouseButtons.Left))
{
//Do Stuff
}
}

How to fire UIElement.TouchDown event on Windows Forms C# on Touchscreen, windows 8

This need to be Windows Forms (winforms) - not WPF. Problem is with single MouseDown event that didn't fire properly on touchscreen on windows 8. MouseDown fire either after touch and move finger or after click - it fire just after you get off your finger. I want to fire it like a normal MouseDown should - after i touch the screen. The solution to that would be that TouchDown event. But, i just cant to handle that event. What I do is that I create class Multitouch like that:
class Moultitouch : UIElement
{
public Moultitouch()
{
this.TouchDown += new EventHandler<System.Windows.Input.TouchEventArgs>(Moultitouch_TouchDown);
}
void Moultitouch_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
{
//it never goes in
}
protected override void OnTouchDown(System.Windows.Input.TouchEventArgs e)
{
base.OnTouchDown(e);
}
}
After that I'm declaring that class in my component which on Touch should handle that event. I did a lot of research on that and 've been trying to do that and nothing so far. I think that "Hosting a WPF Composite Control in Windows Forms" http://msdn.microsoft.com/en-us/library/ms742215.aspx could solve my problem but that solution could be really long and tough. Any help or ideas are really appreciated.
Your issue is that touchscreen fired events and mouse fired events are not the same thing. For example a Winform button can toggle its background colour by handling OnMouseEnter and OnMouseLeave. Code that up on a form and run the cursor in and out of the button and everything works fine. Use a touch screen and run your finger over it in the same way and nothing happens until you release contact with the screen, and then a click event is raised. Not the answer you are looking for, but I am having the same issue with an on-screen keyboard I'm developing for a disabled friend of mine. My research led me here https://blogs.windows.com/buildingapps/2017/05/25/uwp-evolution-touch-development/#02octDx0X3uBHXqm.97 and specifically to the links that provide detail on the ManipulationStarted and ManipulationEnded events. Hope that helps, it has for what I'm trying to achieve.

Click on form does not click underlying control

I have a windows forms application. When I click on a window this activates the form and then I need to click again to call the particular control click event. For example if I click on a button this activates the form and then I need to click the button again.
Is there a way to perform the control click and window activation in one click? Preferably I would want this to work with whatever the clickable control is (menu,button, label etc)
So far I have managed to activate the win form on mouse over and then the control click works. I would like to have the win form activated on click and also run the click command on an underlying control if this has a click event.
Well, here's a way to accomplish what You want (just attach a similar method to Your from's MouseClick event):
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var c = this.GetChildAtPoint(e.Location);
this.InvokeOnClick(c, e);
}
This method has it's drawbacks though - for example the control will be clicked even though it's disabled etc., so You have to make sure the control under the cursor is "clickable" by yourself...

Categories