Recent

Author Topic: TShiftState in MouseDown and MouseUp event handlers  (Read 2525 times)

stefanv

  • Newbie
  • Posts: 2
TShiftState in MouseDown and MouseUp event handlers
« on: April 03, 2019, 04:44:17 pm »
I just ran into an interesting problem, which I've already resolved, but I thought I'd share it here in case it bites anyone else.

I'm working on an app, one feature of which is that you can introduce labels at runtime, and drag them around over your document (kind of like attaching annotations in a PDF viewer). In my MouseDown event handler, I wanted to check that it was the left button that was pressed, and that no modifier keys were being used, so I wrote:

Code: Pascal  [Select][+][-]
  1. if (Button = mbLeft) and (Shift = []) then begin ... end

At the time I wrote this, I was developing on a Raspberry Pi, connected remotely using TightVNC. The code above didn't work because Shift contained ssLeft. So I changed it to:

Code: Pascal  [Select][+][-]
  1. if (Button = mbLeft) and (Shift = [ssLeft]) then begin ... end

After that, it worked as expected. However, when I went to go test the app on the Raspberry Pi's own local X server, it no longer worked. I discovered that there, Shift was actually an empty set, as I had originally expected it to be.

My fix was to rewrite the code like this:

Code: Pascal  [Select][+][-]
  1. if (Button = mbLeft) and (Shift * [ssShift,ssCtrl,ssAlt] = []) then begin ... end
This worked properly in both environments.
« Last Edit: April 03, 2019, 04:46:25 pm by stefanv »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: TShiftState in MouseDown and MouseUp event handlers
« Reply #1 on: April 03, 2019, 05:12:43 pm »
Yes, because part of ShiftState is also NumLock (ssNum) and many others, therefore you must always test:
Code: Pascal  [Select][+][-]
  1. if ssAlt in Shift then ...
or what you did
Code: Pascal  [Select][+][-]
  1. ... (Shift * [ssShift,ssCtrl,ssAlt] = []) ...

Note: there exists handy constant ssModifier defined in unit Controls:
Code: Pascal  [Select][+][-]
  1. ssModifier = {$if defined(darwin) or defined(macos) or defined(iphonesim)} ssMeta {$else} ssCtrl {$endif};
which is ssCtrl everywhere except MacOS.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

stefanv

  • Newbie
  • Posts: 2
Re: TShiftState in MouseDown and MouseUp event handlers
« Reply #2 on: April 03, 2019, 05:17:29 pm »
Thanks Blaazen.

I'm curious though, why there's a difference in the presence or absence of ssLeft depending on which X server I am using.

 

TinyPortal © 2005-2018