mouseclick with keypress

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
steenhagen
Posts: 9
Joined: Sunday 5th February 2023 4:13pm

mouseclick with keypress

Post by steenhagen »

Hello,

How do I test the control key, when clicking on a row in a tableview. If CTRL-key is pressed then do this Else do that.
I can't find it on Internet or manuals.

Bert Steenhagen
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: mouseclick with keypress

Post by cogier »

Hi Bert. Try the attached code. The problem as I see it is that you are trying to grab 2 events at the same time, this does seem to work though. Some help is available here.

bMouseDown As Boolean
TableView1 As TableView

Public Sub Form_Open()

  With Me
    .Width = 500
    .Height = 500
    .Arrangement = Arrange.Vertical
    .Padding = 5
  End With

  With TableView1 = New TableView(Me) As "TableView1"
    .Rows.Count = 30
    .Columns.count = 10
    .Expand = True
  End With

End

Public Sub TableView1_KeyPress()

  Dim CntrlDown As Boolean

  If bMouseDown = True Then
    Try CntrlDown = Key.Control
    CntrlDown = IIf(Error, False, CntrlDown)
    If CntrlDown Then
      Me.Text = "Mouse down and Control key is being held down"
    Else
      Me.Text = "Mouse down but Control key is NOT being held down"
    Endif
  Else
    Me.Text = CString(Time) & " Mouse not down"
  End If

End

Public Sub TableView1_MouseDown()

  bMouseDown = True

End

Public Sub TableView1_MouseUp()

  bMouseDown = False

End
steenhagen
Posts: 9
Joined: Sunday 5th February 2023 4:13pm

Re: mouseclick with keypress

Post by steenhagen »

Hello Cogier,

You have helped me well with your approach. I did it the other way around with the key-event

public CtrlPressed as boolean

Public Sub form_KeyPress()
If Key.ControlKey Then
CtrlPressed = True
Endif
End

Public Sub form_KeyRelease()
CtrlPressed = False
End

Public Sub mytable1_RowClick(Row As Integer)
If CtrlPressed = True Then
do_this
Else
do_the_other_thing
endif
End


Thank you so much,

Bert Steenhagen
steenhagen
Posts: 9
Joined: Sunday 5th February 2023 4:13pm

Re: mouseclick with keypress

Post by steenhagen »

I have made a mistake, it has to be

Public Sub form_KeyPress()
If Key.Control Then
CtrlPressed = True
Endif
End


Key.Control not Key.ControlKey

ControlKey is a constant (16777249), always TRUE !!

Bert Steenhagen
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: mouseclick with keypress

Post by cogier »

ControlKey is a constant (16777249), always TRUE !!
Be careful as this will change depending on whether you are using GTK or QT.

I quote here: -
Never compare the value of this property with a numeric constant, because the key codes may depend on the underlying toolkit.

Always use the constants defined in this class!
User avatar
BruceSteers
Posts: 1560
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: mouseclick with keypress

Post by BruceSteers »

Mouse.class has Control / Shift / Alt properties that state keyboard modifiers...

The properties are not valid in a Click() event but they are in a MouseUp and RowClick() event

Something like this will do the job....


Public Sub TableView1_RowClick(Row As Integer)

  If Mouse.Control Then
    Print "Clicked with Ctrl key"
  Else
    Print "Clicked without Ctrl key"
  Endif

End
If at first you don't succeed , try doing something differently.
BruceS
steenhagen
Posts: 9
Joined: Sunday 5th February 2023 4:13pm

Re: mouseclick with keypress

Post by steenhagen »

Hello Bruce,

The better option.

Thanks for your tip

Bert Steenhagen
Post Reply