D2Editor:registerFilter

From Inspired-Lua Wiki
Jump to navigation Jump to search

registerFilter is a function that is part of D2Editor (2D Editor).

This routine registers a table of handler functions that can filter events before they are sent to the 2D editor widget, or unregisters if nil is passed.

The handlerTable is a table of event handler functions. Any event function described within the handlerTable can be filtered by a function in the handler table.

In the example code below, if the user presses Tab in the text editor ed, the tabKey filter function moves the focus to text editor ed2. Events charIn and arrowKey simply report which key was pressed and then allow the event to pass on through to the text editor.


This has been introduced in TI-Nspire OS 3.2 (Changes).


Syntax

D2Editor.registerFilter(editor, [handlerTable])
( or editor:registerFilter([handlerTable]) )

Parameter Type Description
handlerTable
table table of event functions (like enterKey, tabKey...)

The handlerTable argument is optional, since not passing an argument (nil) is the way to reset (turn off) the 2DEditor's event filtering.

Example

-- Create an editor
ed = D2Editor.newRichText()

-- Register filters for events
ed:registerFilter {
  tabKey = function() ed2:setFocus() return true end,
  charIn = function(ch) print(ch) return false end
  arrowKey = function(key) print(arrowKey) return false end
}

Good to know

  • The event handler functions have to be defined "on their own", like "enterKey", "tabKey" etc, and not like the usual definitions "on.enterKey", "on.tabKey" (this is because in that case, you're defining methods of the "on" table, the global events)
  • The event handler functions in the registerFilter table have to return either true or false : true meaning that no further action will be taken for that event, while false means that the default action for the event will still be executed.