Check if Mouse LButton is down? - delphi

How do I check if the Left button of my mouse is currently pressed down/dragging something(I preffer the first possibility).
I tried Mouse.IsDraging,but no result.
NOTE: I handle mouse messages in my application so its no problem if its a WM,just share a way to accomplish my task.

There is a Windows API function GetAsyncKeyState(), which despite its name is also usable to get the state of the mouse buttons. The linked documentation directly contains the answer to your question:
The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON) which returns TRUE if the mouse buttons have been swapped.
The result type is short, to check for the most significant bit just test whether the value is negative.

OnMouseMove(UINT nFlags, CPoint point)
{
m_LButtonPressed=nFlags & MK_LBUTTON;
CWnd::OnMouseMove(nFlags, point);
}

Related

When typing at the end of the editmask, it will pass the number to the left side until it reaches the last one

Good afternoon,
I'm doing a project in delphi that uses editmask. I'm using the phone mask.
When clicking on edit to write the phone number, it goes to the last field on the right, so it is necessary to go back with the backspace to the beginning of the edit on the left.
i would like to find a way that when the user typed the number in the last field on the right, it was passed to the left. So on until you complete the phone field. It would be possible?
Using an example of what it would look like:
I couldn't think of a way to do it
The component is called TMaskEdit.
Just like anything that bases on TEdit putting the focus onto the control will by default put the text cursor at the end of its content
via keyboard, should .AutoSelect be FALSE and
via mouse if clicking behind any text (by default the text is aligned to the left).
You should have experienced this with the other components already. If you want the text cursor to always be at a certain position upon focusing the control, then do that in such an event handler:
for keyboard use OnEnter:
procedure TForm1.MaskEdit1Enter(Sender: TObject);
begin
(Sender as TMaskEdit).SelStart:= 1; // Second position
end;
and for mouse use OnClick with the same code.
It even works unbound to how the property .AutoSelect is set.
Using Backspace is the worst choice input wise, as it always deletes potential content and needs to be pressed several times to go to the first position. Why not using the Home key instead?

Use nsIEnvironment to figure out if mouse is left or right handed

I want to detect if the user is using their mouse with the left button being primary (e.button == 0) and right as secondary (e.button == 1). Can I do this with nsIEnvironment?
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIEnvironment
Is it possible to figure out how many buttons the users mouse has with nsIEnvironment?
Thanks
You do not have to do check this at all and actually should not check this at all when working with mouse events.
The docs state:
For a mouse configured for left-handed use, the button actions are reversed. In this case, the values are read from right to left.
So you should always assume that event.button == 0 (left) is primary, event.button == 2 (right) is secondary, event.button == 1 is middle click.
If you need this for some other reason: I'm not aware of any way to reliably check this via XPCOM (nsIEnviroment has nothing to do with this at all), as this is an underlying functionality of the desktop enviroment/OS that XPCOM does not readily expose. You'd need to write platform-dependent code either using js-ctypes or binary components.

combobox steal keyboard from main window in pyqt

I'm writing a small pyqt program. I want the main window to to react to arrow movement. I added an event to my MainGui class, keyPressEvent, that handle this. The event work fine as long as I don't press certain buttons such as Key_Up or Key_Down are directed to my (currently only) QComboBox and not to my mainGui. I tried to give the focus to mainGui after each paintEvent but then I need to double click on buttons/comboBox.
Then I tried to use the MousePressEvent to check if a certain element is under the mouse. This work fine with the comboBox, but not with the button.
So, how can I direct key events to the mainGui or give the focus to QButtons?
I used eventFilter to identify when the mouse enter the QPushButton and give it focus:
def eventFilter(self,source,event):
if event.type() == QtCore.QEvent.HoverMove:
if self.execButton.underMouse():
self.execButton.setFocus()
self.keepFocus=False
else :
self.keepFocus=True
keepFocus is a flag I initialized in the __init__ function of the class. I added this part at the paintEvent function
if self.keepFocus:
self.setFocus()
else:
self.keepFocus = True
Now, I keep the focus at the MainGui and I only give it to the button when the mouse hove over it. If I do another action (like pressing a mouse button or a keyboard key) the focus is given back to the MainGui. This will create some buggy filling (For example, I need to press twice a keyboard key before the first response) but this is workable.

How do I detect clicking and double-clicking a node in a TreeView?

How do I track the event of clicking a node or double-clicking a node item (meaning, that textual thing next to the plus sign) in a tree view, but not the treeview itself?
Detect a click or double-click on the control in general with the OnClick and OnDblClick events. To find out which specific node was clicked, use the control's GetNodeAt method with the mouse's current coordinates.
You can get the coordinates as they were at the time of the click with GetMessagePos; reading Mouse.CursorPos will give you the coordinates at the time you read the property, which might be very different from the time of the click if there is any sort of load on the system, and that can lead to detecting clicks at places that aren't even within the bounds of your control.
Use usual OnClick and OnDblClick events and look which is the current node item.
http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.ComCtrls.TCustomTreeView.Selected
See also linked examples.
But better use VirtualTreeView.
http://www.lischke-online.de/VirtualTreeview/VTGallery.php
http://code.google.com/p/virtual-treeview/

TStringGrid dirty hack - Restricting the selection to one single row

I have a heavily modified control based on TStringGrid. I want to allow the user to make selections in this grid but restrict the selection one single row (the current row).
Implementation:
When the user presses the left mouse button I am using a dirty hack: On OnMouseDown event I capture the mouse cursor and keep it on the current row. The user can move the mouse device up and down on its pad but the cursor will not go up or down. It will stay on the current row. When the user releases the button (OnMouseUp event), I release the capture.
However, this hack is as I said very dirty. There are several problems. For example, if the user presses the left mouse button (LMB) and then without releasing that button it presses the right button, the associated pop-up menu will pop but the mouse capture will never be released. So, the mouse will be locked in a screen region until the user will has the brilliant idea to click the LMB one more time to unlock the mouse. This may be a bug in D7. There is a separate post about it here: TStringGrid - OnMouseUp is not called!
There is a elegant way to do this?
Edit:
OnSelectCell is not working. OnSelectCell event is called only once when you click the cell. If you keep the button pressed and move the mouse to expand the selection, OnSelectCell will not be called again.
Indeed MoveCurrent appears only in TCustomGrid.MouseDown.
Use the OnSelectCell event and set the CanSelect var parameter depending on whether the ARow parameter is what you want.
A nice solution
Check the options property on TStringGrid
There is an Option called 'goRangeSelect'
Set this to false using the object inspector
Or programatically this can be done by
StringGrid1.Options - [goRangeSelect];
This answer is not elegant at all, but it works.
My solution is NOT to use the PopupMenu property of the StringGrid. Instead I implemented my own PopUpMenu property. Works almost perfect. There is on small problem, the bottom of the pop-up menu appears next to the cursor and not its top.

Resources