Acquisizione di eventi di input dell'utente
È possibile catturare gli eventi di input dell'utente nell'applicazione. A tale scopo, è possibile implementare un blocco funzione che viene eseguito al verificarsi di eventi utente.
Avviso
Progetto di esempio per l'acquisizione di modifiche variabili tramite input utente
Il progetto di esempio registra le modifiche dei valori delle variabili attivate dall'input dell'utente all'interno della visualizzazione. In questo progetto troverete anche l'implementazione del metodo ValueChanged
.
Cattura della scrittura delle variabili
Quando l'utente completa l'inserimento di un valore (in un campo di input), viene chiuso un evento di controllo di modifica. È possibile catturare questo evento nell'applicazione come segue.
Crea un blocco funzione che implementa l'interfaccia
VisuElems.IEditBoxInputHandler
dalla bibliotecaVisuElemBase
.Passare l'istanza al gestore eventi globale
VisuElems.Visu_Globals.g_VisuEventManager
chiamando il metodoSetEditBoxEventHandler
.
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 sui quali l'utente è invitato a cliccare per immettere il testo.
L'elemento di output del testo è un rettangolo in cui è visualizzato il contenuto della variabile di testo PLC_PRG.stInfo
vengono stampati. La variabile di testo contiene l'ultimo input immesso dall'utente in uno dei campi di input e le informazioni aggiuntive che sono state aggiunte.

Proprietà del rettangolo | |
| |
| |
Proprietà del rettangolo | |
| |
| |
Proprietà del rettangolo per l'output del testo | |
| |
|
Implementazione di PLC_PRG
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
Implementazione di POU
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'));
Cattura degli eventi della tastiera
Quando l'utente preme e rilascia il tasto, viene generato un evento tastiera nella visualizzazione. È possibile catturare questo evento nell'applicazione come segue.
Crea un blocco funzione che implementa l'interfaccia
VisuElems.IKeyEventHandler
dalla bibliotecaVisuElemBase
.Passare l'istanza al gestore eventi globale
VisuElems.Visu_Globals.g_VisuEventManager
chiamando il metodoSetKeyEventHandler
.
Una visualizzazione ha un elemento di output di testo. L'elemento di output di testo è un rettangolo in cui è visualizzato il contenuto della variabile di testo. PLC_PRG.stInfo
vengono stampati. La variabile di testo contiene informazioni sull'ultimo tasto premuto dall'utente.
Proprietà del rettangolo per l'output del testo | |
| |
|
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 funzionale POU
FUNCTION_BLOCK POU IMPLEMENTS VisuElems.IKeyEventHandler (* no further declarations, no implementation code *)
Implementazione del metodo VariableWritten
del blocco funzionale 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 delle variabili attivate da eventi di input
Tutti gli elementi di visualizzazione che modificano il valore di una variabile tramite input utente chiamano l'interfaccia IValueChangedListener
Con questa interfaccia è possibile registrare le modifiche dei valori e poi elaborarle a livello di programmazione.
Avviso
Progetto di esempio per l'acquisizione di modifiche variabili tramite input utente
Il progetto di esempio registra le modifiche dei valori delle variabili attivate dall'input dell'utente all'interno della visualizzazione. In questo progetto troverete anche l'implementazione del metodo ValueChanged
.
Implementare un blocco funzionale (esempio:
POU
) che implementa l'interfacciaIValueChangedListener
.FUNCTION_BLOCK POU IMPLEMENTS VisuElems.IValueChangedListener
Nell'albero dei dispositivi, il Valore modificato il metodo viene inserito sotto il blocco funzione.
In un programma (esempio: PLC_PRG), implementare il codice IEC che registra l'interfaccia.
VisuElems.g_itfValueChangedListenerManager.AddValueChangedListener(itfValueChangedListener)
PLC_PRG ottiene tutte le variazioni di valore tramite il Valore modificato metodo.
Ora è possibile registrare ed elaborare le modifiche dei valori.
Cattura 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 generato un evento. È possibile catturare questo evento nell'applicazione come segue.
Crea un blocco funzione che implementa l'interfaccia
VisuElems.IInputOnElementEventHandler
dalla bibliotecaVisuElemBase
.Passare l'istanza al gestore eventi globale
VisuElems.Visu_Globals.g_VisuEventManager
chiamando il metodoSetInputOnElementEventHandler
.
Una visualizzazione presenta un rettangolo che reagisce all'input. Ad esempio, l'attivazione/disattivazione di una variabile è stata configurata con OnMouseDown
Quando l'elemento viene cliccato con un mouse o un input touch, si verifica un evento HandleInputOnElementEvent
è sollevato.
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 funzionale POU
FUNCTION_BLOCK POU IMPLEMENTS VisuElems.VisuElemBase.IInputOnElementEventHandler (* no further declarations, no implementation code *)
Implementazione del metodo HandleInputOnElementEvent
del blocco funzionale 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) *)