User Tools

Site Tools


14.2.2 Mouse and events

Mouse events are events that refer to events of visible control elements (components) associated with the mouse. This means that you can only use the mouse object in connection with an event that refers to such mouse events:

Public Sub Form_Open()
  Print Mouse.X
End ' Form_Open()

The above instruction to display the value of Mouse.X in a console therefore produces the error message: No mouse event data. Understandable, because the procedure Form_Open() has nothing to do with the mouse.

The following section of the source code is correct:

Public Sub Form_MouseDown() ' MouseDown() is a mouse event
  Print Mouse.X

In this way, the system determines internally in Gambas components and complex control elements such as a drawing area and other containers which (logical) sub-element of the container was clicked on. Mouse. X is compared with the absolute position of the container plus the relative position of the sub-element to its container, and the overview of events in a class captures all events related to the mouse - even if this is not immediately apparent from the name of some events:

  • MouseDown, MouseUp, MouseMove
  • Enter, Leave
  • MouseWheel, MouseDrag, Drag, Drag, DragLeave, DragMove

The events of the last line are listed here and described in detail in other chapters (→ chapter 14.2.3 MouseWheel). Examples 1-3

These procedures show you how to use selected events - MouseWheel, MouseDown, MouseUp and MouseMove - for 3 different components:

Public Sub dwgKS1_MouseWheel()
If Mouse.Delta = +1 Then ' Mouse wheel forward? Enlarge picture (free definition)
     If fZoom < 300 Then
        fZoom = fZoom + 5
        KS_RP_G_Drawing() ' Drawing of coordinate system, grid and graph of the function
     If fZoom > 20 Then
        fZoom = fZoom - 5
  Endif ' Mouse.Delta = +1 ?

The 'Control_MouseWheel()' event exists for selected components. This event is triggered when the mouse wheel is moved or pressed while the mouse cursor is over the component. The event returns the value Mouse.Delta of type Float. The value is +1 if the mouse wheel has been turned forwards.

As an alternative to IF Mouse.Delta = +1 THEN… you can also use IF Mouse.Forward THEN… as an alternative to IF Mouse.

PUBLIC SUB Form_MouseDown()
  IF Mouse.Left THEN
     iDeltaX = Mouse.X
     iDeltaY = Mouse.Y
  ENDIF ' Mouse.Left ?
END ' Form_MouseDown()
Public Sub btnGetInformation_MouseUp()
End ' btnGetInformation_MouseUp()
PUBLIC SUB Form_MouseMove()
  IF Mouse.Left THEN
     FMain.Move(FMain.X - iDeltaX + Mouse.X, FMain.Y - iDeltaY + Mouse.Y)
  ENDIF ' Left mouse button pressed during mouse movement?
END ' Form_MouseMove() Example 4 - Use of mouse and special keys

Since the component WebView1 (gb.qt4.webkit) already uses the mouse wheel to scroll vertically through the web page, the zoom function had to be switched on or off with a switch in order to be able to work with the event MouseWheel(). The control key (Ctrl, Ctrl) is used as a switch. The event component_MouseWheel () refers to the WebView component mentioned above. The zoom factor is only changed if the CTRL key is held down at the same time as the mouse wheel is turned, otherwise the original scroll function for the content of the web page would be active:

[1] Public Sub WebView1_MouseWheel()
[2]   If Mouse.Control Then ' CTRL key (additional) pressed?
[3]      Select Case Mouse.Delta
[4]      Case -1
[5]         If WebView1.Zoom < 2.26 Then
[6]            WebView1.Zoom = Round(WebView1.Zoom - Mouse.Delta / 4, -2)
[7]         Endif
[8]      Case +1
[9]         If WebView1.Zoom > 0.61
[10]            WebView1.Zoom = Round(WebView1.Zoom - Mouse.Delta / 10, -2)
[11]         Endif
[12]      End Select
[13]   Endif ' Mouse.Control = True?
[14] End ' WebView1_MouseWheel()

You can use the Mouse.Middle property to determine whether the middle mouse button - the mouse wheel - was pressed over a component.

[1] Public Sub WebView1_MouseDown()
[2]   If Mouse.Middle Then WebView1.Zoom = 1
[3] End ' WebView1_MouseDown()
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k14/k14.2/k14.2.2/start.txt · Last modified: 11.02.2022 (external edit)

Page Tools