Catturare gli eventi di input dell'utentePermalink

È possibile catturare gli eventi di input dell'utente nell'applicazione. A tale scopo, è possibile implementare un blocco funzione che viene eseguito quando si verificano eventi utente.

Catturare la scrittura delle variabili

Quando l'utente completa l'inserimento di un valore (in un campo di input), viene chiuso un evento del controllo di modifica. È possibile catturare questo evento nell'applicazione come segue.

  1. Creare un blocco funzione che implementi l'interfaccia ‎VisuElems.IEditBoxInputHandler‎ dalla libreria ‎VisuElemBase‎.

  2. Passare l'istanza al gestore di eventi globale ‎VisuElems.Visu_Globals.g_VisuEventManager‎ chiamando il metodo ‎SetEditBoxEventHandler‎.

Esempio

Una visualizzazione ha due campi di input per ‎iInput_A‎ e ‎rInput_B‎ e un elemento di output di testo.

I campi di input sono rettangoli su cui l'utente deve fare clic per inserire il testo.

L'elemento di output del testo è un rettangolo in cui viene stampato il contenuto della variabile di testo ‎PLC_PRG.stInfo‎. La variabile testo contiene l'ultimo dato inserito dall'utente in uno dei campi di input e le informazioni aggiuntive che sono state aggiunte.

_visu_img_hook_control_event

Proprietà del rettangolo ‎iInput_A‎

«Testi  Testo»

‎iInput_A: %i‎

«Variabili di testo  Variabile di testo»

PLC_PRG.iInput_A

Proprietà del rettangolo ‎rInput_B‎

«Testi  Testo»

‎iInput_B: %i‎

«Variabili di testo  Variabile di testo»

PLC_PRG.rInput_B

Proprietà del rettangolo per l'output del testo

«Testi  Testo»

‎%s‎

«Variabili di testo  Variabile di testo»

PLC_PRG.stInfo

‎PLC_PRG‎ implementazione

PROGRAM PLC_PRG
VAR_INPUT
	iInput_A:INT;  (* Used in the visualization as user input variable*)
	rInput_B:REAL; (* Used in the visualization as user input variable*)
	stInfo : STRING;  (* Informs about the user input via the edit control field; 
		String gets composed by method 'VariableWritten; 
		Result is displayed in the lower rectangle of the visualization *)
END_VAR
VAR
	inst : POU;
	bFirst : BOOL := TRUE;
END_VAR

IF bFirst THEN
	bFirst := FALSE;
	VisuElems.Visu_Globals.g_VisuEventManager.SetEditBoxEventHandler(inst); 
	(* Call of method VariableWritten *)
END_IF

‎POU‎ implementazione

FUNCTION_BLOCK POU IMPLEMENTS VisuElems.IEditBoxInputHandler
(* no further declarations, no implementation code *)

Metodo ‎VariableWritten‎ assegnato a ‎POU‎

METHOD VariableWritten : BOOL
(* provides some information always when an edit control field is closed in the visualization, that is a variable gets written by user input in one of the upper rectangles *)
VAR_INPUT
	pVar : POINTER TO BYTE;
	varType : VisuElems.Visu_Types;
	iMaxSize : INT;
	pClient : POINTER TO VisuElems.VisuStructClientData;
END_VAR

// String stInfo, which will be displayed in the lower rectangle, is composed here
PLC_PRG.stInfo := 'Variable written; type: ';
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, INT_TO_STRING(varType));
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, ', adr: ');
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, DWORD_TO_STRING(pVar));
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, ', by: ');
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, SEL(pClient^.globaldata.clienttype = VisuElems.Visu_ClientType.Targetvisualization,'other visu', 'targetvisu'));

Catturare gli eventi della tastiera

Quando l'utente preme e rilascia il tasto, nella visualizzazione viene attivato un evento tastiera. È possibile catturare questo evento nell'applicazione come segue.

  1. Creare un blocco funzione che implementi ‎VisuElems.IVisuUserEventManager‎ dalla libreria ‎VisuElemBase‎.

  2. Passare l'istanza al gestore di eventi globale ‎VisuElems.Visu_Globals.g_VisuEventManager‎ chiamando il metodo ‎SetKeyEventHandler‎.

Esempio

Una visualizzazione ha un elemento di output di testo. L'elemento di output del testo è un rettangolo in cui viene stampato il contenuto della variabile di testo ‎PLC_PRG.stInfo‎. La variabile text contiene informazioni sull'ultimo tasto premuto dall'utente.

Proprietà del rettangolo per l'output del testo

«Testi  Testo»

‎%s‎

«Variabili di testo  Variabile di testo»

PLC_PRG.stInfo

Implementazione del programma ‎PLC_PRG‎

PROGRAM PLC_PRG
VAR_INPUT
	stInfo : STRING;
END_VAR
VAR
	inst : POU;
	bFirst : BOOL := TRUE;
END_VAR

IF bFirst THEN
	bFirst := FALSE;
	VisuElems.Visu_Globals.g_VisuEventManager.SetKeyEventHandler(inst);
END_IF

Implementazione del blocco funzione ‎POU‎

FUNCTION_BLOCK POU IMPLEMENTS VisuElems.IKeyEventHandler
(* no further declarations, no implementation code *)

Implementazione del metodo ‎VariableWritten‎ del blocco funzione ‎POU‎

/// This method will be called after a key event is released. 
/// RETURN:
/// TRUE - When the handler has handled this event and it should not be handled by someone else
/// FALSE - When the event is not handled by this handler
METHOD HandleKeyEvent : BOOL
VAR_INPUT
	/// Event type. The value is true if a key-up event was released.
	bKeyUpEvent : BOOL;
	/// Key code
	dwKey : DWORD;
	/// Modifier. Possible values:
	/// VISU_KEYMOD_SHIFT : DWORD := 1;
	/// VISU_KEYMOD_ALT : DWORD := 2;
	/// VISU_KEYMOD_CTRL : DWORD := 4;
	dwModifiers : DWORD;
	/// Pointer to the client structure were the event was released
	pClient : POINTER TO VisuStructClientData;
END_VAR
VAR
END_VAR

PLC_PRG.stInfo := 'KeyEvent up: ';
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, BOOL_TO_STRING(bKeyUpEvent));
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, ', key: ');
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, DWORD_TO_STRING(dwKey));
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, ', modifier: ');
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, DWORD_TO_STRING(dwModifiers));
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, ', by: ');
PLC_PRG.stInfo := CONCAT(PLC_PRG.stInfo, SEL(pClient^.globaldata.clienttype = 
                                             VisuElems.Visu_ClientType.Targetvisualization, 
																																													'other visu', 'targetvisu'));

Registrazione delle modifiche del valore della variabile innescate da eventi di ingresso

Tutti gli elementi di visualizzazione che modificano il valore di una variabile tramite l'input dell'utente chiamano l'interfaccia ‎IValueChangedListener‎. Con questa interfaccia, le variazioni di valore possono essere registrate e quindi elaborate in modo programmatico.

  1. Implementare un blocco funzione (esempio: ‎POU‎) che implementi l'interfaccia ‎IValueChangedListener‎.

    ‎FUNCTION_BLOCK POU IMPLEMENTS VisuElems.IValueChangedListener‎

    Nella struttura del dispositivo, il metodo «ValueChanged» è inserito sotto il blocco funzione.

  2. In un programma (esempio: «PLC_PRG»), implementare il codice IEC che registra l'interfaccia.

    ‎VisuElems.g_itfValueChangedListenerManager.AddValueChangedListener(itfValueChangedListener)‎

    «PLC_PRG» riceve tutte le modifiche di valore tramite il metodo «ValueChanged».

    Ora è possibile registrare ed elaborare le variazioni di valore.

Acquisizione di input su elementi con opzioni di input

Quando un utente fa clic su un elemento che reagisce all'input (ad esempio, un rettangolo con una configurazione di input), viene attivato un evento. È possibile catturare questo evento nell'applicazione come segue.

1. Creare un blocco funzione che implementi l'interfaccia ‎VisuElems.IInputOnElementEventHandler‎ dalla libreria ‎VisuElemBase‎.

2. Passare l'istanza al gestore di eventi globale ‎VisuElems.Visu_Globals.g_VisuEventManager‎ chiamando il metodo ‎SetInputOnElementEventHandler‎.

Esempio 9. Esempio

Una visualizzazione ha un rettangolo che reagisce agli input. Ad esempio, l'attivazione di una variabile è stata configurata con ‎OnMouseDown‎. Quando l'elemento viene cliccato con il mouse o con un input tattile, viene attivato l'evento ‎HandleInputOnElementEvent‎.

Implementazione della funzione ‎VisuInit‎

‎FUNCTION VisuInit : BOOL ‎

‎// Set the input on element event handler VisuElems.VisuElemBase.g_VisuEventManager.SetInputOnElementEventHandler(PLC_PRG.evInputOnElementEventHandler); ‎

Implementazione del blocco funzione ‎POU‎

‎FUNCTION_BLOCK POU IMPLEMENTS VisuElems.VisuElemBase.IInputOnElementEventHandler (* no further declarations, no implementation code *) ‎

Attuazione del metodo ‎HandleInputOnElementEvent‎ del blocco funzione POU

‎(* This method will be called when an input on a visualization element was executed. ‎

‎RETURN: ‎

‎TRUE - When the handler has handled this event and it should not be handled by someone else ‎

‎FALSE - When the event is not handled by this handler*)‎

‎ METHOD HandleInputOnElementEvent : BOOL ‎

‎VAR_INPUT event : VisuElems.VisuStructInputOnElementEvent;‎

‎END_VAR ‎

‎IF event.eType = VisuElems.VisuEnumInputOnElementType.MouseDown THEN ‎

‎SysProcessExecuteCommand('python D:\Beep.py', 0); ‎

‎END_IF‎

‎ (* Content Beep.py: ‎

‎import winsound ‎

‎freq=500 duration=200 winsound.Beep(freq,duration) ‎

‎*) ‎