Gebeurtenissen

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 11:55, 18 March 2013 by DiGro (Talk | contribs)

Jump to: navigation, search
Book.png


Apache OpenOffice dialoogvensters en formulieren zijn gebaseerd op een gebeurtenis georiënteerd programmeermodel waar u gebeurtenis-behandelaars kunt toewijzen aan de besturingselementen. Een gebeurtenis-behandelaar voert een voorgedefinieerde procedure uit als een bepaalde actie plaatsvindt, zelfs als die actie een andere gebeurtenis is. U kunt ook documenten of geopende gegevensbronnen bewerken met gebeurtenis-behandeling net als andere besturingselementen benaderen.

Apache OpenOffice besturingselementen herkennen verschillende typen van gebeurtenissen die kunnen worden geactiveerd in verschillende situaties. Deze typen gebeurtenis kunnen worden verdeeld in vier groepen:

  • Gecontroleerd door muis: Gebeurtenissen die reageren op acties van de muis (bijvoorbeeld, eenvoudige muis-bewegingen of een klik op een bepaalde locatie op het scherm).
  • Gecontroleerd door toetsenbord: Gebeurtenissen die worden geactiveerd door bepaalde toets-aanslagen.
  • Aanpassingen van de focus: Gebeurtenissen die Apache OpenOffice uitvoert als besturingselementen worden geactiveerd of gedeactiveerd.
  • Specifieke gebeurtenissen voor besturingselementen: Gebeurtenissen die alleen gebeuren in relatie met bepaalde besturingselementen.

Als u werkt met gebeurtenissen, verzeker u er dan van dat u het bijbehorend dialoogvenster maakt in de ontwikkelomgeving van Apache OpenOffice en dat het de vereiste besturingselementen of documenten (als u de gebeurtenissen wilt toepassen op een formulier) bevat.

De OpenOffice.org BASIC ontwikkelomgeving (IDE)

De bovenstaande afbeelding toont de Apache OpenOffice BASIC ontwikkelomgeving met een dialoogvenster dat twee keuzelijsten bevat. U kunt de gegevens vanuit de ene lijst naar de andere verplaatsen met behulp van de knoppen tussen de twee keuzelijsten.

Als u het ontwerp op het scherm wilt weergeven, dan zou u de geassocieerde procedures voor Apache OpenOffice BASIC moeten maken zodat die kunnen worden aangeroepen door de gebeurtenisbehandelaars. Hoewel u deze procedures in elke module kunt gebruiken, is het beter hun gebruik tot twee modules te beperken. Om uw code makkelijker te kunnen lezen, zou u betekenisvolle namen moeten geven aan deze procedures. Direct springend naar een algemene programmaprocedure vanuit een macro kan onduidelijke code opleveren. In plaats daarvan, om onderhoud van de code en probleemoplossing te vereenvoudigen, zou u een andere procedure moeten maken om te dienen als startpunt voor gebeurtenisbehandeling – zelfs als het alleen maar één enkele aanroep naar de doelprocedure uitvoert.

De code in het volgende voorbeeld verplaatst een invoer vanuit de linker naar de rechter lijst van een dialoogvenster.

Sub cmdSelecteren_Initiated
 
   Dim lstItems As Object
   Dim lstSelectie As Object
 
   lstItems = Dlg.getControl("lstItems")
   lstSelectie = Dlg.getControl("lstSelectie")
 
   If lstItems.SelectedItem > 0 Then
     lstSelectie.AddItem(lstItems.SelectedItem, 0)
     lstItems.removeItems(lstItems.SelectItemPos, 1)
   Else
     Beep
   End If
 
End Sub

Als deze procedure werd gemaakt in Apache OpenOffice BASIC kunt u die toewijzen aan een vereiste gebeurtenis met behulp van het venster voor de eigenschappen van de editor voor dialoogvensters.

Het dialoogvenster Actie toewijzen

Het dialoogvenster voor toewijzing somt alle beschikbare gebeurtenissen op. Om een macro toe te wijzen aan een gebeurtenis:

  1. Selecteer de gebeurtenis
  2. Klik op Macro...
  3. Blader naar en selecteer de macro die u wilt toewijzen
  4. Klik op OK

Parameters

Het plaatsvinden van een bepaalde gebeurtenis is niet altijd genoeg voor een gewenste reactie. Aanvullende informatie kan nodig zijn. Bijvoorbeeld: u zou mogelijk de positie op het scherm waar op de muis werd gedrukt nodig kunnen hebben om een muisklik te verwerken.

In Apache OpenOffice BASIC kunt u parameters voor objecten gebruiken om meer informatie aan een procedure te verschaffen over een gebeurtenis, bijvoorbeeld:

Sub GebeurtenisVerwerken(Event As Object)
 
End Sub

De structuur en eigenschappen van het object Event zijn afhankelijk van het type gebeurtenis dat de aanroep van de procedure activeert.

Ongeacht het type gebeurtenis, verschaffen alle objecten toegang tot het relevante besturingselement en diens model. Het besturingselement kan worden benaderd met behulp van Event.Source en het model ervan met behulp van Event.Source.Model. Het dialoogvenster kan worden benaderd met behulp van Event.Source.Context.

U kunt deze eigenschappen gebruiken om een gebeurtenis te activeren binnen een gebeurtenisbehandelaar.


Gebeurtenissen voor de muis

Apache OpenOffice BASIC herkent de volgende gebeurtenissen voor de muis:

Muis verplaatst moved
gebruiker verplaatst de muis
Muis verplaatst met ingedrukte toets
gebruiker sleept muis met ingedrukte toets
Mouse button pressed
user presses a mouse button

Template:Documentation/Note

Mouse button released
user releases a mouse button
Mouse outside
user moves mouse outside of the current window

The structure of the associated event objects is defined in the com.sun.star.awt.MouseEvent structure which provides the following information:

Buttons (short)
button pressed (one or more constants in accordance with com.sun.star.awt.MouseButton)
X (long)
X-coordinate of mouse, measured in pixels from the top left corner of the control element
Y (long)
Y-coordinate of mouse, measured in pixels from the top left corner of the control element
ClickCount (long)
number of clicks associated with the mouse event (if Apache OpenOffice can respond fast enough, ClickCount is also 1 for a double-click because only an individual event is initiated)

The constants defined in com.sun.star.awt.MouseButton for the mouse buttons are:

LEFT
left mouse button
RIGHT
right mouse button
MIDDLE
middle mouse button

The following example outputs the mouse position as well as the mouse button that was pressed:

Sub MouseUp(Event As Object)
 
   Dim Msg As String
 
   Msg = "Keys: "
   If Event.Buttons AND com.sun.star.awt.MouseButton.LEFT Then
     Msg = Msg & "LEFT "
   End If
 
   If Event.Buttons AND com.sun.star.awt.MouseButton.RIGHT Then
     Msg = Msg & "RIGHT "
   End If
 
   If Event.Buttons AND com.sun.star.awt.MouseButton.MIDDLE Then
     Msg = Msg & "MIDDLE "
   End If
 
   Msg = Msg & Chr(13) & "Position: "
   Msg = Msg & Event.X & "/" & Event.Y
   MsgBox Msg
 
End Sub
Documentation note.png VBA : The VBA Click and Doubleclick events are not available in Apache OpenOffice Basic. Instead use the Apache OpenOffice Basic MouseUp event for the click event and imitate the Doubleclick event by changing the application logic.


Keyboard Events

The following keyboard events are available in Apache OpenOffice Basic:

Key pressed
user presses a key.
Key released
user releases a key

Both events relate to logical key actions and not to physical actions. If the user presses several keys to output a single character (for example, to add an accent to a character), then Apache OpenOffice Basic only creates one event.

A single key action on a modification key, such as the Shift key or the Alt key does not create an independent event.

Information about a pressed key is provided by the event object that Apache OpenOffice Basic supplies to the procedure for event handling. It contains the following properties:

KeyCode (short)
code of the pressed key (default values in accordance with com.sun.star.awt.Key)
KeyChar (String)
character that is entered (taking the modification keys into consideration)

The following example uses the KeyCode property to establish if the Enter key, the Tab key, or one of the other control keys has been pressed. If one of these keys has been pressed, the name of the key is returned, otherwise the character that was typed is returned:

Sub KeyPressed(Event As Object)
 
   Dim Msg As String
 
   Select Case Event.KeyCode
   Case com.sun.star.awt.Key.RETURN
     Msg = "Return pressed"
   Case com.sun.star.awt.Key.TAB
     Msg = "Tab pressed"
   Case com.sun.star.awt.Key.DELETE
     Msg = "Delete pressed"
   Case com.sun.star.awt.Key.ESCAPE
     Msg = "Escape pressed"
   Case com.sun.star.awt.Key.DOWN
     Msg = "Down pressed"
   Case com.sun.star.awt.Key.UP
     Msg = "Up pressed"
   Case com.sun.star.awt.Key.LEFT
     Msg = "Left pressed"
   Case com.sun.star.awt.Key.RIGHT
     Msg = "Right pressed"
   Case Else
     Msg = "Character " & Event.KeyChar & " entered"
   End Select
   MsgBox Msg
 
End Sub

Information about other keyboard constants can be found in the API Reference under the com.sun.star.awt.Key group of constants.

Focus Events

Focus events indicate if a control element receives or loses focus. You can use these events to, for example, determine if a user has finished processing a control element so that you can update other elements of a dialog. The following focus events are available:

When receiving focus
element receives focus
When losing focus
element loses focus

The Event objects for the focus events are structured as follows:

FocusFlags (short)
cause of focus change (default value in accordance with com.sun.star.awt.FocusChangeReason )
NextFocus (Object)
object that receives focus (only for the When losing focus event)
Temporary (Boolean)
the focus is temporarily lost

Control Element-Specific Events

In addition to the preceding events, which are supported by all control elements, there are also some control element-specific events that are only defined for certain control elements. The most important of these events are:

When Item Changed
the value of a control element changes
Item Status Changed
the status of a control element changes
Text modified
the text of a control element changes
When initiating
an action that can be performed when the control element is triggered (for example, a button is pressed)

When you work with events, note that some events, such as the When initiating event, can be initiated each time you click the mouse on some control elements (for example, on radio buttons). No action is performed to check if the status of the control element has actually changed. To avoid such “blind events”, save the old control element value in a global variable, and then check to see if the value has changed when an event is executing.

The When initiating event is also noteworthy for the following reasons:

  • This event is initiated by either a key-press or a mouse button. Thus, it provides a consistent interface for users who navigate by mouse or by keyboard.
  • When the Repeat property of a command button is set to True, this event is the one which is repeatedly sent, as long as the triggering action (key down or mouse-button down) remains in effect.

The properties for the Item Status Changed event are:

Selected (long)
currently selected entry
Highlighted (long)
currently highlighted entry
ItemId (long)
ID of entry


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools