mouseclick with keypress

Post

Posted
Rating:
#1 (In Topic #996)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Code (gambas)

  1. bMouseDown As Boolean
  2. TableView1 As TableView
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   With Me
  7.     .Width = 500
  8.     .Height = 500
  9.     .Arrangement = Arrange.Vertical
  10.     .Padding = 5
  11.  
  12.   With TableView1 = New TableView(Me) As "TableView1"
  13.     .Rows.Count = 30
  14.     .Columns.count = 10
  15.     .Expand = True
  16.  
  17.  
  18. Public Sub TableView1_KeyPress()
  19.  
  20.   Dim CntrlDown As Boolean
  21.  
  22.   If bMouseDown = True Then
  23.     Try CntrlDown = Key.Control
  24.     CntrlDown = IIf(Error, False, CntrlDown)
  25.     If CntrlDown Then
  26.       Me.Text = "Mouse down and Control key is being held down"
  27.     Else
  28.       Me.Text = "Mouse down but Control key is NOT being held down"
  29.     Endif
  30.   Else
  31.     Me.Text = CString(Time) & " Mouse not down"
  32.   End If
  33.  
  34.  
  35. Public Sub TableView1_MouseDown()
  36.  
  37.   bMouseDown = True
  38.  
  39.  
  40. Public Sub TableView1_MouseUp()
  41.  
  42.   bMouseDown = False
  43.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
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
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
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
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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!
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
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….

Code (gambas)

  1.  
  2. Public Sub TableView1_RowClick(Row As Integer)
  3.  
  4.     Print "Clicked with Ctrl key"
  5.   Else
  6.     Print "Clicked without Ctrl key"
  7.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Trainee
 Hello Bruce,

The better option.

Thanks for your tip

Bert Steenhagen
Online now: No Back to the top
1 guest and 0 members have just viewed this.