Distinguish between single and double click with matplotlib - python

I'm trying to catch single clicks and double clicks on my figure.
As stated in another answer, the event contains event.dblclick which is False or True, at least in Version 1.4.2, so double clicks can be got.
The only problem is that it's not easy to distinguish between a single click and a double click because when double-clicking the event gets fired twice. The first time it's with event.dblclick=False and the second time it's with event.dblclick=True.
Is there any solution to this? I saw that the same problem regarding qt is discussed here.

You need a software debouncer.
Basically, you start a timer on the 1st click. If the timer runs out, then you proceed to process the single-click event. If a 2nd click is detected within the timer, process a double-click event.
This can actually be expanded to n-clicks if needed. I've found some uses for triple-click events.
Here is one implemented in wxPython. Should be relatively easy to port to matplotlib.
Also, if you're on Windows, I would recommend using the user's double-click speed for your timer (Control Panel: Mouse). You can access it with:
get_double_click_time():
""" Gets the Windows double click time in ms """
from ctypes import windll
return int(windll.user32.GetDoubleClickTime())
I haven't yet figured out how to grab the dclick time from Mac or Linux (but I also don't have a need to).

Related

double clicked and clicked events emiting ad the same time PySide [duplicate]

I have a QAbstractItemView that needs to react to single and double click events. The actions are different depending on whether it was single clicked or double clicked. The problem that is occurring is that the single click event is received prior to the double click event.
Is there a recommended way/best practice for distinguishing between the two? I don't want to perform the single click action when the user has actually double clicked.
I am using Qt 4.6
It's a good UI design to make sure your single-clicks and double-clicks are conceptually related:
Single-Click: select icon
Double-Click: select icon and open it
Single-Click: select color
Double-Click: select color and open palette editor
Notice how in these examples the single-click action is actually a subset of the double-click. This means you can go ahead and do your single-click action normally and just do the additional action if the double-click comes in.
If your user interface does something like:
Single-Click: select icon
Double-Click: close window
Then you are setting your users up to fail. Even if they remember what single-clicking does versus double-clicking all the time, it's very easy to accidentally move your mouse too far while double-clicking or wait too long.
Edit:
I'm sorry to hear that.
In that case, I found these two articles useful:
Logical consequences of the way
Windows converts single-clicks into
double-clicks
Implementing
higher-order clicks
You can find answer in the thread titled Double Click Capturing on QtCentre forum;
You could have a timer. Start the
timer in the releaseEvent handler and
make sure the timeout is long enough
to handle the double click first.
Then, in the double click event
handler you can stop the timer and
prevent it from firing. If a double
click handler is not triggered, the
timer will timeout and call a slot of
your choice, where you can handle the
single click. This is of course a
nasty hack, but has a chance to work.
wysota
Using PySide which is the Python binding of Qt 4.8 I saw that single clicks deliver a QEvent.MouseButtonPress event and double clicks deliver a single QEvent.MouseButtonPress event closely followed by a QEvent.MouseButtonDblClick. The delay is approximately about 100ms on Windows. That means you still have a problem if you need to differentiate between single and double clicks.
The solution needs another QTimer with a slightly higher delay than the inbuilt delay (adding some overhead). If you observe a QEvent.MouseButtonPress event you start your own timer, in case of timeout it is a single click. In case of a QEvent.MouseButtonDblClick it is a double click and you stop the timer to avoid counting as single click.

How to disable keyboard shortcut Alt+F4, in PyQt5? [duplicate]

Ctrl+Escape is a global Windows shortcut for opening main system menu. But I would like my Qt application to use this shortcut without triggering Windows main menu. I know it is probably a bad idea to override system shortcuts in general, but I would like to use this shortcut is a very limited use case.
This usecase is as follows. I have a popup window containing several rows or items. This window is opened by Ctrl+Tab and while the user holds Ctrl and keep pressing Tab, the current rows are cycled through. When the user releases Ctrl, the current row is used for some operation... But sometimes it happens that user presses Ctrl+Tab and then realizes he does not want to continue. He usually presses Escape while still holding Ctrl. And then it triggers Windows system menu and normal user gets confused, choleric user get angry... which is a bad thing. In other words I would like to be able to close the popup window when user presses Ctrl+Escape. How to do that? It is even possible?
If I write the code using this shortcut like any other short, it does not work and it always triggers Windows main menu.
As I understand it, Qt will typically not receive the key event if the underlying window system has intercepted it. For example even QtCreator cannot override system-wide shortcuts.
This question is almost a duplicate of: C++/Qt Global Hotkeys
While that question is asking specifically to capture shortcuts in a hidden/background application, I think the basic concept is the same -- capture shortcuts before the window system processes them.
From that answer, UGlobalHotkey seems pretty good, and the How to use System-Wide Hotkeys in your Qt application blog post could be useful for your limited-use case (but read the comments on that blog post about fixing the example).
Also found:
https://github.com/mitei/qglobalshortcut
https://github.com/Skycoder42/QHotkey (looks like a more detailed version of above)

How to simulate button presses in pyside?

What I'm trying to do:
I have a Python(PySide) and Qt/QML UI that today responds to keyboard input (actually it's an IR remote control, but the input events are received as though they were coming from a keyboard).
I want to be able to also respond to mouse events. So where today the user uses arrow keys to navigate to a particular button and presses OK (i.e., Return) to activate the handling for that button, I would like them to just be able to click the mouse on that button and get the same handling behavior.
What I have so far:
I've already got Keys.onReturnPressed: handling in my QML code that does what I need to do when the user presses OK/Return. And I've added MouseArea { ... onClicked: {...} ... } QML code that recognizes when I click on a given control. So I already see in my log when the mouse click events occur.
My question:
How do I tie them together? I want to make the onClicked: behavior just generate some kind of event (a signal, maybe?) that causes the onReturnPressed: handling to be invoked. (I'm not at all averse to passing events through the Python side of things if that's what it takes to make this work.)
(I guess I should mention here that the existing code includes some base classes (is that the right terminology here?) that can define behavior across ALL controls of a certain type (e.g., Buttons) in the system. So while each of the many, many Buttons has its own onReturnPressed: code providing its unique handling, my objective is to have a single onClicked: handler in the base class that makes all Buttons respond to clicks as they do to Return presses today.)
Can anyone point me in the right direction?
BTW: Yes, I'm aware that there's a second problem here, too, of navigation. That is, even once I've turned the mouse click into a press
of the Return key, I still have to solve the problem of associating it
with the right control on the screen.
I sort of didn't want to muddy the waters by asking too many things at once.
I'll get to that one
when I've got this one in hand. (...unless you've got a simple solution already up your sleeve... In that case I'm all ears.)

Bug with refresh in python curses

I'm writing a program in curses and sometimes happens that if I leave the program opened and I use other terminal tabs for a while, when I go using the program again it seems like it has refreshed something and something has disappeared... I cannot show pics or screenshots because I haven't understood yet well when and how it happens... Is there a way to prevent or fix this?
screen.getch reads from stdscr, and if it refreshes (due to any change on the screen), will overwrite boxes. You could change that to box.getch, as I did in scroll page by page or line by line using python curses
The manual page for getch says
If the window is not a pad, and it has been moved or modified since the last call to wrefresh, wrefresh will be called before another character is read.
In your sample program you used
screen.keypad( 1 )
which only applies to reading from the standard screen. If you read from the box window, you should set the keypad flag on that:
box.keypad( 1 )
The manual page for keypad says
The default value for keypad is FALSE
that is, it is the default for each window.
A curses program with multiple windows can choose to read from different windows at different times. There is only one input buffer for each screen, but the side-effect of refreshing the current window makes it simpler to manage updates to the windows. (For complicated window stacking order, you would use the panel library rather than rely upon this side-effect).

How to record mouse clicks on mac to be replayed by an apple script?

I am writing a script to automate repeatedly registering new users for a website (not boosting metrics, not what you think!). I can boil down the process of registering to a series of mouse clicks and typing. I know there are some macro recorders that will let me record how I use the GUI and repeat it, but I need to type something a little different every time - however the mouse clicks are always the same.
Imagining script to look kindof like:
username = "something"
for i in range(0,100):
playback recorded series of mouse clicks A
type username + str(i)
type some other stuff
playback recorded series of mouse clicks B
But I can't find a good tool to record a series of mouse clicks so that I can play them back from either an applescript or a python script.
I found a useful script (http://www.bluem.net/en/mac/cliclick/) I can call that takes in 2 coordinates and clicks. So even if I could find a tool to record the coordinates of mouseclicks that would work!
You can also run JavaScripts in browsers with something like tell application "Safari" to tell document 1 to do JavaScript or tell application "Google Chrome" to tell tab 1 of window 1 to execute javascript.
tell application "Safari" to tell document 1
do JavaScript "document.querySelectorAll('.mainnavs ul li:nth-child(2) a')[0].click()"
delay 1
repeat until do JavaScript "document.readyState" is "complete"
delay 0.1
end repeat
do JavaScript "document.getElementById('tagfilter').value = 'aa'"
end tell
System Events has commands for simulating clicks and keystrokes:
delay 1
tell application "System Events" to tell process "Safari"
click at {76, 117}
repeat with i from 1 to 3
keystroke "aa" & i & return
end repeat
keystroke "a" using command down
end tell
You can see the positions of UI elements from Accessibility Inspector.app. It is part of Xcode, but it can also be downloaded from developer.apple.com/downloads.
If you want to get the screen coordinates of where you want to perform mouse clicks, you can use a free tool I made called MouseTools found here. I made it for just this purpose. You'll want to do something like I show in the example #1 applescript near the bottom of the page. Just move your mouse to a position on the screen and run the applescript to get the coordinates. Note: you'll want to move your mouse to a location and press command-r (while the applescript is frontmost) to run the script in AppleScript Editor.
In addition, I found that clicking at screen coordinates, as Lauri Ranta showed in her applescript using System Events, often does not reliably work. System Events is good at clicking in the Finder however it often has trouble clicking in the window of an application like Safari. If you find that problem too then you can also use the MouseTools to perform the mouse clicks. Again, this is why I created the program and there are examples of how to do this on the web page.
I hope it helps. Good luck.
How about Carsten Blum's Clicclick?
His link
It is a shell app which will emulate mouse clicks at arbitrary screen coordinates. This will work with applescript.
Hopefully you are doing test automation!
Have you looked into using something like Selenium WebDriver instead?

Categories