# JSEvent

## Overview

The `JSEvent` object serves as the primary argument for user-event callbacks, encapsulating key details about application-triggered events. It provides information such as event type, source element or form, position, and any associated data, enabling developers to handle interactions dynamically and efficiently. Constants like `ACTION`, `DATACHANGE`, and `DOUBLECLICK` help identify specific event types, while methods such as `getType()`, `getSource()`, and `getElementName()` give precise context for each event.

In addition to identifying event origins and types, the object supports positional data with methods like `getX()` and `getY()`, and tracks the timing of occurrences using `getTimestamp()`. It also features a `data` property to carry event-specific payloads, enhancing customization options. This makes `JSEvent` a flexible and powerful tool for implementing responsive, user-driven functionality in applications.

## Constants Summarized

| Type                                                     | Name                               | Summary                                                          |
| -------------------------------------------------------- | ---------------------------------- | ---------------------------------------------------------------- |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [ACTION](#action)                  | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [DATACHANGE](#datachange)          | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [DOUBLECLICK](#doubleclick)        | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [FOCUSGAINED](#focusgained)        | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [FOCUSLOST](#focuslost)            | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [FORM](#form)                      | Constant returned by JSEvent.                                    |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [MODIFIER\_ALT](#modifier_alt)     | Constant for the ALT modifier that can be returned by JSEvent.   |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [MODIFIER\_CTRL](#modifier_ctrl)   | Constant for the CTRL modifier that can be returned by JSEvent.  |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [MODIFIER\_META](#modifier_meta)   | Constant for the META modifier that can be returned by JSEvent.  |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [MODIFIER\_SHIFT](#modifier_shift) | Constant for the SHIFT modifier that can be returned by JSEvent. |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [NONE](#none)                      | Constant returned by JSEvent.                                    |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [RIGHTCLICK](#rightclick)          | Constant returned by JSEvent.                                    |

## Properties Summarized

| Type                                                     | Name          | Summary                                                                                                           |
| -------------------------------------------------------- | ------------- | ----------------------------------------------------------------------------------------------------------------- |
| [Object](/reference/servoycore/dev-api/js-lib/object.md) | [data](#data) | A data object that specific events can set, a user can set data back to the system for events that supports this. |

## Methods Summarized

| Type                                                     | Name                                  | Summary                                                                                      |
| -------------------------------------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------- |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [getElementName()](#getelementname)   | returns the name of the element, can be null if the form was the source of the event.        |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [getFormName()](#getformname)         | returns the name of the form the element was placed on.                                      |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [getModifiers()](#getmodifiers)       | Returns the modifiers of the event, see JSEvent.                                             |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [getName()](#getname)                 | Returns the name of the event which was triggered                                            |
| [Object](/reference/servoycore/dev-api/js-lib/object.md) | [getSource()](#getsource)             | returns the source component/element of the event.                                           |
| [Date](/reference/servoycore/dev-api/js-lib/date.md)     | [getTimestamp()](#gettimestamp)       | Returns the time the event occurred.                                                         |
| [String](/reference/servoycore/dev-api/js-lib/string.md) | [getType()](#gettype)                 | returns the event type see the JSEvents constants what it can return.                        |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [getX()](#getx)                       | Returns the x position of the event, relative to the component that fired it, if applicable. |
| [Number](/reference/servoycore/dev-api/js-lib/number.md) | [getY()](#gety)                       | Returns the y position of the event, relative to the component that fired it, if applicable. |
| void                                                     | [stopPropagation()](#stoppropagation) | stopPropagation is used in case of multiple event listeners (added via application.          |

## Constants Detailed

### ACTION

Constant returned by JSEvent.getType() in a method that is attached to an onAction event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.ACTION)
{
	// its an action event.
}
```

### DATACHANGE

Constant returned by JSEvent.getType() in a method that is attached to an onDataChange event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.DATACHANGE)
{
	// its a data change event
}
```

### DOUBLECLICK

Constant returned by JSEvent.getType() in a method that is attached to an onDoubleClick event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.DOUBLECLICK)
{
	// its a double click event.
}
```

### FOCUSGAINED

Constant returned by JSEvent.getType() in a method that is attached to an onFocusGained or the forms onElementFocusGained event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.FOCUSGAINED)
{
	// its a focus gained event.
}
```

### FOCUSLOST

Constant returned by JSEvent.getType() in a method that is attached to an onFocusLost or the forms onElementFocusLost event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.FOCUSLOST)
{
	// its a focus lost event.
}
```

### FORM

Constant returned by JSEvent.getType() in a method that is attached to a form event (like onShow) or command (like onDeleteRecord)

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.FORM)
{
	// its a form event or command
}
```

### MODIFIER\_ALT

Constant for the ALT modifier that can be returned by JSEvent.getModifiers();

**Type**\
[Number](/reference/servoycore/dev-api/js-lib/number.md)

**Sample**

```js
//test if the SHIFT modifier is used.
if (event.getModifiers() & JSEvent.MODIFIER_SHIFT)
{
	//do shift action
}
```

### MODIFIER\_CTRL

Constant for the CTRL modifier that can be returned by JSEvent.getModifiers();

**Type**\
[Number](/reference/servoycore/dev-api/js-lib/number.md)

**Sample**

```js
//test if the SHIFT modifier is used.
if (event.getModifiers() & JSEvent.MODIFIER_SHIFT)
{
	//do shift action
}
```

### MODIFIER\_META

Constant for the META modifier that can be returned by JSEvent.getModifiers();

**Type**\
[Number](/reference/servoycore/dev-api/js-lib/number.md)

**Sample**

```js
//test if the SHIFT modifier is used.
if (event.getModifiers() & JSEvent.MODIFIER_SHIFT)
{
	//do shift action
}
```

### MODIFIER\_SHIFT

Constant for the SHIFT modifier that can be returned by JSEvent.getModifiers();

**Type**\
[Number](/reference/servoycore/dev-api/js-lib/number.md)

**Sample**

```js
//test if the SHIFT modifier is used.
if (event.getModifiers() & JSEvent.MODIFIER_SHIFT)
{
	//do shift action
}
```

### NONE

Constant returned by JSEvent.getType() if the event is not used in a known event or command.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.NONE)
{
	// type is not set.
}
```

### RIGHTCLICK

Constant returned by JSEvent.getType() in a method that is attached to an onRightClick event.

**Type**\
[String](/reference/servoycore/dev-api/js-lib/string.md)

**Sample**

```js
if (event.getType() == JSEvent.RIGHTCLICK)
{
	// its a right click event.
}
```

## Properties Detailed

### data

A data object that specific events can set, a user can set data back to the system for events that supports this.

**Type**\
[Object](/reference/servoycore/dev-api/js-lib/object.md)

**Sample**

```js
// A client design method that handles ondrag
if (event.getType() == JSEvent.ONDRAG)
{
     // the data is the selected elements array
     var elements = event.data;
     // only start a client design drag when there is 1 element
     if (elements.length == 1)
     {
     	return true;
     }
}

// code for a data drag method
event.data = "drag me!";
return DRAGNDROP.COPY;

// code for a data drop method
var data = event.data;
elements[event.getElementName()].setText(data);
return true;
```

## Methods Detailed

### getElementName()

returns the name of the element, can be null if the form was the source of the event.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) a String representing the element name.

**Sample**

```js
if (event.getElementName() == 'myElement')
{
    elements[event.getElementName()].bgcolor = '#ff0000';
}
```

### getFormName()

returns the name of the form the element was placed on.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) a String representing the form name.

**Sample**

```js
forms[event.getFormName()].myFormMethod();
```

### getModifiers()

Returns the modifiers of the event, see JSEvent.MODIFIER\_XXXX for the modifiers that can be returned.

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) an int which holds the modifiers as a bitset.

**Sample**

```js
//test if the SHIFT modifier is used.
if (event.getModifiers() & JSEvent.MODIFIER_SHIFT)
{
	//do shift action
}
```

### getName()

Returns the name of the event which was triggered

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) name of event as string

**Sample**

```js
var name = event.getName();
```

### getSource()

returns the source component/element of the event.\
If it has a name the getElementName() is the name of this component.

**Returns:** [Object](/reference/servoycore/dev-api/js-lib/object.md) an Object representing the source of this event.

**Sample**

```js
// cast to runtime text field (change to anoter kind of type if you know the type)
/** @type {RuntimeTextField} */
var source = event.getSource();
var sourceDataProvider = source.getDataProviderID();
```

### getTimestamp()

Returns the time the event occurred.

**Returns:** [Date](/reference/servoycore/dev-api/js-lib/date.md) a Date when this event happened.

**Sample**

```js
event.getTimestamp();
```

### getType()

returns the event type see the JSEvents constants what it can return.\
Plugins can create events with there own types.

**Returns:** [String](/reference/servoycore/dev-api/js-lib/string.md) a String representing the type of this event.

**Sample**

```js
if (event.getType() == JSEvent.ACTION)
{
	// its an action event.
}
```

### getX()

Returns the x position of the event, relative to the component that fired it, if applicable.\
For example drag'n'drop events will set the x,y positions.

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) an int representing the X position.

**Sample**

```js
var x = event.getX();
var xPrevious = previousEvent.getX();
var movedXPixels = x -xPrevious;
```

### getY()

Returns the y position of the event, relative to the component that fired it, if applicable.\
For example drag'n'drop events will set the x,y positions.

**Returns:** [Number](/reference/servoycore/dev-api/js-lib/number.md) an int representing the Y position.

**Sample**

```js
var y = event.getY();
var yPrevious = previousEvent.getY();
var movedYPixels = y -yPrevious;
```

### stopPropagation()

stopPropagation is used in case of multiple event listeners (added via application.addEventListener). When application.fireEventListeners is called you can use this api to stop executing further listeners.

**Returns:** void

**Sample**

```js
event.stopPropagation()
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.servoy.com/reference/servoycore/dev-api/application/jsevent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
