JavaFX: Right click on Mac (Control + Click) not being detected. Any fixes? - macos

I have a list view with items that I am allowing to be double clicked and right clicked (to delete the item). Why does control clicking not work on a mac? Thanks in advance.
Edit: My code is
listview.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
if (event.getButton().equals(MouseButton.PRIMARY))
{
if (event.getClickCount() == 2)
{
System.out.println("Double clicked");
System.out.println("clicked on " + listview.getSelectionModel().getSelectedItem());
}
}
if(event.getButton().equals(MouseButton.SECONDARY))
{
System.out.println("Right click");
}
}
});
My trackpad is set up as secondary button with two finger tap.

For anyone else looking this up. It is correct that two finger clicking will register as a MouseButton.SECONDARY event, but I think you should also check for Ctrl + MouseButton.PRIMARY since holding the control key is a common method for emulating a right click. So the if statement should be :
if ( event.getButton() == MouseButton.SECONDARY || e.isControlDown() ) {
// DO RIGHT CLICK ACTION
}

Related

Should I be thinking differently about cancelling the back button in Xamarin Forms

I have a Prism based Xamarin Forms app that contains an edit page that is wrapped in a Navigation page so there is a back button at top left on both Android and iOS. To avoid the user accidentally losing an edit in progress by accidentally clicking the back button (in particular on Android) we want to prompt them to confirm that they definitely want to cancel.
Thing is, this seems like something that is not baked in to Xamarin forms. You can override OnBackButtonPressed in a navigation page, but that only gets called for the hardware/software back button on Android. There are articles detailing techniques to intercept the actual arrow button at the top left on Android (involving overriding OnOptionsItemSelected in the Android MainActivity), but on iOS I'm not sure it is even possible.
So I can't help but wonder if I am going about this the wrong way? Should I not be intercepting the top left / hardware / software back button in this way? Seems like a pretty common thing to do (e.g. press back when editing a new contact in the android built in Contacts app and you get a prompt) but it really feels like I am fighting the system here somehow.
There are previous questions around this, most relevant appears to be How to intercept Navigation Bar Back Button Clicked in Xamarin Forms? - but I am looking for some broad brush suggestions for an approach here. My objective is to show the user a page with the <- arrow at top left for Android, "Cancel" for iOS but I would like to get some views about the best way to go about it that does not involve me fighting against prism / navigation pages / xamarin forms and (where possible) not breaking the various "best practices" on Android and iOS.
After going down the same path as you and being told not to prevent users from going back, I decided on showing an alert after they tap the back button (within ContentPage.OnDisappearing()) that says something like Would you like to save your work?.
If you go with this approach, be sure to use Application.MainPage.DisplayAlert() instead of just this.DisplayAlert() since your ContentPage might not be visible at that point.
Here is how I currently handle saving work when they click the back button (I consolidated a good bit of code and changed some things):
protected override async void OnDisappearing() {
base.OnDisappearing();
// At this point the page is gone or is disappearing, but all properties are still available
#region Auto-save Check and Execution
/*
* Checks to see if any edits have been made and if a save is not in progress, if both are true, it asks if they want to save, if yes, it checks for validation errors.
* If it finds them, it marks it as such in the model before saving the model to the DB and showing an alert stating what was done
*/
if(!_viewModel.WorkIsEdited || _viewModel.SaveInProgress) { //WorkIsEdited changes if they enter/change data or focus on certain elements such as a Picker
return;
}
if(!await Application.Current.MainPage.DisplayAlert("ALERT", "You have unsaved work! Would you like to save now?", "Yes", "No")) {
return;
}
if(await _viewModel.SaveClaimErrorsOrNotAsync()) { //The return value is whether validation succeeds or not, but it gets saved either way
App.SuccessToastConfig.Message = "Work saved successfully. Try saving it yourself next time!";
UserDialogs.Instance.Toast(App.SuccessToastConfig);
} else if(await Application.Current.MainPage.DisplayAlert("ERROR", "Work saved successfully but errors were detected. Tap the button to go back to your work.", "To Work Entry", "OK")) {
await Task.Delay(200); //BUG: On Android, the alert above could still be displayed when the page below is pushed, which prevents the page from displaying //BUG: On iOS 10+ currently the alerts are not fully removed from the view hierarchy when execution returns (a fix is in the works)
await Application.Current.MainPage.Navigation.PushAsync(new WorkPage(_viewModel.SavedWork));
}
#endregion
}
What you ask for is not possible. The back button tap cannot be canceled on iOS even in native apps. You can do some other tricks like having a custom 'back' button, but in general you shouldn't do that - you should instead have a modal dialog with the Done and Cancel buttons (or something similar).
If you use xamarin forms that code it is work.
CrossPlatform source
public class CoolContentPage : ContentPage
{
public Action CustomBackButtonAction { get; set; }
public static readonly BindableProperty EnableBackButtonOverrideProperty =
BindableProperty.Create(nameof(EnableBackButtonOverride), typeof(bool), typeof(CoolContentPage), false);
public bool EnableBackButtonOverride{
get { return (bool)GetValue(EnableBackButtonOverrideProperty); }
set { SetValue(EnableBackButtonOverrideProperty, value); }
}
}
}
Android source
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == 16908332)
{
var currentpage = (CoolContentPage)
Xamarin.Forms.Application.
Current.MainPage.Navigation.
NavigationStack.LastOrDefault();
if (currentpage?.CustomBackButtonAction != null)
{
currentpage?.CustomBackButtonAction.Invoke();
return false;
}
return base.OnOptionsItemSelected(item);
}
else
{
return base.OnOptionsItemSelected(item);
}
}
public override void OnBackPressed()
{
var currentpage = (CoolContentPage)
Xamarin.Forms.Application.
Current.MainPage.Navigation.
NavigationStack.LastOrDefault();
if (currentpage?.CustomBackButtonAction != null)
{
currentpage?.CustomBackButtonAction.Invoke();
}
else
{
base.OnBackPressed();
}
}
iOS source
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (((CoolContentPage)Element).EnableBackButtonOverride)
{
SetCustomBackButton();
}
}
private void SetCustomBackButton()
{
var backBtnImage = UIImage.FromBundle("iosbackarrow.png");
backBtnImage = backBtnImage.ImageWithRenderingMode
(UIImageRenderingMode.AlwaysTemplate);
var backBtn = new UIButton(UIButtonType.Custom)
{
HorizontalAlignment =
UIControlContentHorizontalAlignment.Left,
TitleEdgeInsets =
new UIEdgeInsets(11.5f, 15f, 10f, 0f),
ImageEdgeInsets =
new UIEdgeInsets(1f, 8f, 0f, 0f)
};
backBtn.SetTitle("Back", UIControlState.Normal);
backBtn.SetTitleColor(UIColor.White, UIControlState.Normal);
backBtn.SetTitleColor(UIColor.LightGray, UIControlState.Highlighted);
backBtn.Font = UIFont.FromName("HelveticaNeue", (nfloat)17);
backBtn.SetImage(backBtnImage, UIControlState.Normal);
backBtn.SizeToFit();
backBtn.TouchDown += (sender, e) =>
{
// Whatever your custom back button click handling
if(((CoolContentPage)Element)?.
CustomBackButtonAction != null)
{
((CoolContentPage)Element)?.
CustomBackButtonAction.Invoke();
}
};
backBtn.Frame = new CGRect(
0,
0,
UIScreen.MainScreen.Bounds.Width / 4,
NavigationController.NavigationBar.Frame.Height);
var btnContainer = new UIView(
new CGRect(0, 0,
backBtn.Frame.Width, backBtn.Frame.Height));
btnContainer.AddSubview(backBtn);
var fixedSpace =
new UIBarButtonItem(UIBarButtonSystemItem.FixedSpace)
{
Width = -16f
};
var backButtonItem = new UIBarButtonItem("",
UIBarButtonItemStyle.Plain, null)
{
CustomView = backBtn
};
NavigationController.TopViewController.NavigationItem.LeftBarButtonItems = new[] { fixedSpace, backButtonItem };
}
using in xamarin forms
public Page2()
{
InitializeComponent();
if (EnableBackButtonOverride)
{
this.CustomBackButtonAction = async () =>
{
var result = await this.DisplayAlert(null, "Go back?" Yes go back", "Nope");
if (result)
{
await Navigation.PopAsync(true);
}
};
}
}

Hide and Show Textview with button click in Dialog fragment

I am new to programming and i hope someone can help provide me a simple code
I have a dialog fragment with a textview and a button.
It loaded with both visible.
when I click on the button, I need to hide the textview.
when I click it again, I need the textview to be shown.
I tried textview1.setVisibility(View.GONE);
The textview still stays there, i think i am missing a refresh method???
Thanks
btnclick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textview1.setVisibility(View.GONE);
}
});
Hi, Use if else loop it will solve this issue for example refer this code
boolean s=true;
btnclick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(s) {
textview1.setVisibility(View.GONE);
s=false;
}
else {
textview1.setVisibility(View.VISIBLE);
s=true;
}
} });

How to detect right-click on script UI button?

Using Adobe InDesign's extendscript (javascript language), how can I add an onClick function for both left AND right click events?
Adding the left-click event is easy. How do I add the right-click event?
[post answer update (much thanks to Josh Voights for finding that)]
If anyone is interested, I wanted to use this in such a way to apply the handler to the button like this, which works perfectly:
whatbutton.addEventListener("click", function(p){
if(!p.shiftKey){
if (p.button ==2) {
alert("right click");
}else{
alert("left click");
}
}else{
if (p.button ==2) {
alert("shift right click");
}else{
alert("shift left click");
}
}
Here's a code sample from kahrel.plus.com/indesign/scriptui.html that includes watching for a right click. Credit to #fabiantheblind in this stackoverflow answer.
var w =new Window ("dialog");
var b = w.add ("button", undefined, "Qwerty");
b.addEventListener("click", function (k){whatsup (k)});
function whatsup (p)
{
if(p.button == 2){ $.writeln ("Right-button clicked.") }
if(p.shiftKey){ $.writeln ("Shift key pressed.") }
$.writeln ("X: "+ p.clientX);
$.writeln ("Y: "+ p.clientY);
}
w.show ();

Flush mouse events to a disabled widget

I have a Dialog with few buttons. When I disable the dialog, and I click on a child button, nothing happens. But as soon as I enable the Dialog again, the mouse event for the button is handled.
Does disabling a Dialog simply delays handling of any mouse events for its child widgets until enabled again?
I tried installing event filters for child widgets when dialog is disabled and then removing the event filters when enabled again. But it provides the same behavior. As soon as dialog is enabled i.e. event filters for child widgets removed, the mouse event (when disabled) are handled.
Can someone please help whats wrong here?
void MyDialog::changeEvent(QEvent *e)
{
if(e->type() == QEvent::EnabledChange)
{
if(isEnabled())
{
qDebug("Filters removed!\n");
ui->pbtnOption1->removeEventFilter(this);
ui->pbtnOption2->removeEventFilter(this);
ui->pbtnOption3->removeEventFilter(this);
}
else
{
qDebug("Filters installed\n");
ui->pbtnOption1->installEventFilter(this);
ui->pbtnOption2->installEventFilter(this);
ui->pbtnOption3->installEventFilter(this);
}
}
}
bool MyDialog::eventFilter(QObject *obj, QEvent *e)
{
if(obj == ui->pbtnOption1 || obj == ui->pbtnOption2 || obj == ui->pbtnOption3)
{
if(e->type() == QEvent::MouseButtonPress)
{
qDebug("MousePress Event!\n");
return true;
}
else
return false;
}
return QDialog::eventFilter(obj, e);
}

MouseClick on Label

I am trying to register a click on a label, but i can't get it to work.
So far I've tried to set the SelectionAdapter to the label but click-events aren't fired.
Labels are not selectable Controls SelectionAdapter won't work for it. Try adding a MouseListener.
For sake of completeness, I'll just add this code sample:
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseUp(MouseEvent event) {
super.mouseUp(event);
if (event.getSource() instanceof Label) {
Label label = (Label)event.getSource();
System.out.println("Label was clicked: " + label.getText());
}
}
});

Resources