I want to make a button that immediately respond when you place finger on it.
Button b1=FindViewById<Button>(Resource.Id.b1);
b1.Click += delegate {
m1.SeekTo(0);
m1.Start();
};
What this doing is that you get the respond (event is turning on) when you place off your finger from the button.
you should use Touch event.
Example :
private void Btn_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
if (e.Event.Action == Android.Views.MotionEventActions.Down)
{
//do something immediately after touch the button
}
if (e.Event.Action == Android.Views.MotionEventActions.Up)
{
//do something after placing off your finger
}
}
Related
I currently have a NotifyIcon as part of a Windows Form application. I would like to have the form show/hide on a double click of the icon and show a balloon tip when single clicked. I have the two functionalities working separately, but I can't find a way to have the app distinguish between a single click and double click. Right now, it treats a double click as two clicks.
Is there a way to block the single click event if there is a second click detected?
Unfortunately the suggested handling of MouseClick event doesn't work for NotifyIcon class - in my tests e.MouseClicks is always 0, which also can be seen from the reference source.
The relatively simple way I see is to delay the processing of the Click event by using a form level flag, async handler and Task.Delay :
bool clicked;
private async void OnNotifyIconClick(object sender, EventArgs e)
{
if (clicked) return;
clicked = true;
await Task.Delay(SystemInformation.DoubleClickTime);
if (!clicked) return;
clicked = false;
// Process Click...
}
private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
clicked = false;
// Process Double Click...
}
The only drawback is that in my environment the processing of the Click is delayed by half second (DoubleClickTime is 500 ms).
There are 2 different kinds of events.
Click/DoubleClick
MouseClick / MouseDoubleClick
The first 2 only pass in EventArgs whereas the second pass in a MouseEventArgs which will likely allow you additional information to determine whether or not the event is a double click.
so you could do something like;
obj.MouseClick+= MouseClick;
obj.MouseDoubleClick += MouseClick;
// some stuff
private void MouseClick(object sender, MouseEventArgs e)
{
if(e.Clicks == 2) { // handle double click }
}
It is enough to register a Click event and then handle single and double clicks from it.
int clickCount;
async void NotifyIcon_Click( object sender, EventArgs e ) {
if( clickCount > 0 ) {
clickCount = 2;
return;
}
clickCount = 1;
await Task.Delay( SystemInformation.DoubleClickTime );
if( clickCount == 1 ) {
// Process single click ...
} else if( clickCount == 2 ) {
// Process double click ...
}
clickCount = 0;
}
I am using visual studios to create a Windows IoT app. On the GUI of the app there is a button that when held down with a mouse click or a key being pressed it performs an action, in this case a text box changes its text and a motor is run. When the button is released the text box changes its text and the motor is stopped.
I created a button in the .xml file and button_holding event in the .cs, but this doesn't perform any of the actions.
On the GUI of the app there is a button that when held down with a
mouse click or a key being pressed it performs an action, in this case
a text box changes its text and a motor is run. When the button is
released the text box changes its text and the motor is stopped.
You can use PointerPressedEvent and PointerReleasedEvent to achieve it. This is the code works for me:
public MainPage()
{
this.InitializeComponent();
myButton.AddHandler(PointerPressedEvent, new PointerEventHandler(myButton_PointerPressed), true);
myButton.AddHandler(PointerReleasedEvent, new PointerEventHandler(myButton_PointerReleased), true);
}
private void myButton_PointerPressed(object sender, PointerRoutedEventArgs e)
{
myText.Text = "Running...";
}
private void myButton_PointerReleased(object sender, PointerRoutedEventArgs e)
{
myText.Text = "Stopped";
}
You can use Holding event to get similar MouseUp and MouseDown in traditional WinForms, like so
private void Button_Holding(object sender, HoldingRoutedEventArgs e)
{
if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
{
//do things when button on hold
}
else if (e.HoldingState == Windows.UI.Input.HoldingState.Completed ||
e.HoldingState == Windows.UI.Input.HoldingState.Canceled)
{
//do things when button release
}
}
I've used backrequest event like below code and on appbar title back button or mobile device hardware button I should click twice on button till event work.
What should I do to it work with just one click?
SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e) =>
{
bool handeled = e.Handled;
if (Frame.CanGoBack && !handeled)
{
handeled = true;
Frame.GoBack();
}
e.Handled = handeled;
};
if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
Windows.Phone.UI.Input.HardwareButtons.BackPressed += (sender, e) =>
{
bool handeled = e.Handled;
if (Frame.CanGoBack && !handeled)
{
handeled = true;
Frame.GoBack();
}
e.Handled = handeled;
};
}
I had the same issue. Every time I wanted to navigate back, I had to press the back button twice. Until I found out, that the SplitView was responsible for this problem. I had to close it, before navigating.
For example, if you want to move from Page1 to another page and then go back, use something like this on Page1:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
MySplitView.IsPaneOpen = false;
}
I hope it helps, even if you said that on a blank project (obvious without a Hamburger menu) the result was the same.
How can I get the type of pressed pointer (left mouse down or right mouse down) in a Metro style C# app? I didn't find a MouseLeftButtonDown event handler in any Metro style UI element. I should use PointerPressed event instead, but I don't know how can i get which button was pressed.
PointerPressed is enough to handle mouse buttons:
void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Check for input device
if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
var properties = e.GetCurrentPoint(this).Properties;
if (properties.IsLeftButtonPressed)
{
// Left button pressed
}
else if (properties.IsRightButtonPressed)
{
// Right button pressed
}
}
}
You can use the following event to determine what pointer is used and what button is pressed.
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
if (ptrPt.Properties.IsLeftButtonPressed)
{
//Do stuff
}
if (ptrPt.Properties.IsRightButtonPressed)
{
//Do stuff
}
}
Working on a UWP project and previous answers like
Properties.IsLeftButtonPressed/IsRightButtonPressed did not work for me. Those values are always false. I realized during the Debugging that Properties.PointerUpdateKind was changing according to mouse button. Here is the result which worked for me:
var properties = e.GetCurrentPoint(this).Properties;
if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonReleased)
{
}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonReleased)
{
}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.MiddleButtonReleased)
{
}
There are more options in PointerUpdateKind like ButtonPressed varities of the ones in the example and XButton varities e.g. XButton1Pressed, XButton2Released etc.
I need to update a control only whenever the mouse moves over it with the left mouse button pressed. I would normally simply check for the e.Button property, but it is unavailable in the MouseEnter event.
void MyControl_MouseEnter(object sender, EventArgs e)
{
// MouseEventArgs needed to check this
// if (e.Button == MouseButtons.Left)
// {
// update MyControl
// }
}
How would you accomplish this?
Use the static Control.MouseButtons property. For example:
private void panel1_MouseEnter(object sender, EventArgs e) {
if (Control.MouseButtons == MouseButtons.Left) {
// etc...
}
}
This is very difficult to get going, whatever the user clicks on to get the mouse button pressed is going to capture the mouse, preventing the control's MouseEnter event from firing. It is also UI that's completely undiscoverable to the user. Do consider a better mouse trap.
Here's one (crude) way to do it. It will change the form's title text to whatever mouse button was pressed as you dragged your mouse on to button1. You can reference Control.MouseButtons to see which button is in a pressed state. Here's some more info on MSDN.
public partial class Form1 : Form
{
MouseButtons _buttons;
public Form1()
{
InitializeComponent();
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_buttons != System.Windows.Forms.MouseButtons.None)
{
this.Text = _buttons.ToString();
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
_buttons = Control.MouseButtons;
}
}
I found the answer in another question here on SO:
How can I detect a held down mouse button over a PictureBox?
You will need to use a message filter. Implement the PreFilterMessage of the IMessageFilter interface, and assign an instance using Application.AddMessageFilter.
You will have to interpret windows messages yourself... that is not kind of difficult, but it will require some work.
The implementation may look like this:
if (m.Msg == 0x200)
{
int x, y;
x = m.LParam.ToInt32() & 0xFFFF;
y = m.LParam.ToInt32() >> 16;
if ((m.WParam.ToInt32() & 2) != 0)
{
// here, the left mouse button is pressed, and you can use the coords
// and see if the mouse is over the control you want.
}
}
I just implemented something like this today, tested only in Chrome but works rather nicely. Basic concept is that you capture mousemove only between mousedown and mouseup, as follows:
var image = document.getElementById('my_image');
image.addEventListener('mousedown', function(e) {
e.currentTarget.addEventListener('mousemove', doMyStuff);
});
image.addEventListener('mouseup', function(e) {
e.currentTarget.removeEventListener('mousemove', doMyStuff);
});
function doMyStuff(e) {
// do what you want, the mouse is moving while the user is holding the button down
}
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles
Button1.MouseDown
If Control.MouseButtons = MouseButtons.Left Then
Label1.Text = "Left"
ElseIf Control.MouseButtons = MouseButtons.Right Then
Label1.Text = "Right"
ElseIf Control.MouseButtons = MouseButtons.Middle Then
Label1.Text = "Middle"
Else
Label1.Text = "lelse"
End If
End Sub