You can log user input to satisfy auditing requirements or other purposes. This sample appends all of the keys entered by a user to a log file.
Before you run the sample, make sure a session is open in InfoConnect.
Get a handle to the terminal and create event handlers |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Text; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; using System.IO; namespace LogUserInput { class Program { static void Main(string[] args) { //Get the last activated instance of InfoConnect Application app = MyReflection.ActiveApplication; //Make sure InfoConnect is running if (app != null) { //Get the active view IFrame frame = (IFrame)app.GetObject("Frame"); IView view = (IView)frame.SelectedView; //Check for an Ibm terminal if (view.Control.ToString() == "Attachmate.Reflection.Emulation.IbmHosts.IbmTerminal") { IIbmTerminal terminal = (IIbmTerminal)view.Control; IIbmScreen screen = terminal.Screen; screen.BeforeSendControlKey += new BeforeSendControlKeyEventHandler(screen_BeforeSendControlKey); screen.BeforeSendKeys += new BeforeSendKeysEventHandler(screen_BeforeSendKeys); } } else { Console.WriteLine("Could not find an instance of InfoConnect"); } Console.ReadKey(); } } } |
Get a handle to a terminal and create event handlers |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.IO; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.OpenSystems; namespace FileTransfer { class Program { static void Main(string[] args) { //Get the last activated instance of InfoConnect Application app = MyReflection.ActiveApplication; //Make sure InfoConnect is running if (app != null) { IFrame frame = (IFrame)app.GetObject("Frame"); IView view = (IView)frame.SelectedView; if (view.Control.ToString() == "Attachmate.Reflection.Emulation.OpenSystems.Terminal") { ITerminal terminal = (ITerminal)view.Control; IScreen screen = terminal.Screen; screen.KeysSent += new KeysSentEventHandler(screen_keysSent); screen.ControlKeySending += new ControlKeySendingEventHandler(screen_ControlkeySending); } } else { Console.WriteLine("Cannot find an instance of InfoConnect"); } Console.ReadKey(); } } } |
Handle the BeforeSendKeys to append each key entered on the screen to the buffer |
Copy Code
|
---|---|
//Use a string for a buffer static public string userInput = " "; static void screen_BeforeSendKeys(object sender, BeforeSendKeysEventArgs args) { //Append each key entered on the screen to the buffer userInput += args.Keys; } |
Handle the BeforeSendKeys to append each key entered on the screen to the buffer |
Copy Code
|
---|---|
//Use a string for a buffer static public string userInput = " "; static void screen_keysSent(object sender, KeysSentEventArgs args) { //Append each key entered on the screen to the buffer userInput += args.Key; } |
Handle the BeforeSendControlKey event to append the buffer to the log file |
Copy Code
|
---|---|
static void screen_BeforeSendControlKey(object sender, BeforeSendControlKeyEventArgs args) { IIbmScreen screen = (IIbmScreen)sender; string LogFile = @"C:\users\" + Environment.UserName + @"\Documents\userInputLog.txt"; //If the Transmit control key is sent to change the screen, write the user input in the buffer //to a log file if (args.Key.Equals(ControlKeyCode.Transmit) == true) { //Open a file and append the data entered on the screen StreamWriter w = File.AppendText(LogFile); w.WriteLine(userInput); //Add a divider to indicate the end of the screen w.WriteLine("..................................................................."); w.Close(); //Reset the buffer for the next screen userInput = " "; } //for the Tab control key and other control keys, create a new line in the buffer else { userInput += "\r\n"; } } |
Handle the BeforeSendControlKey event to append the buffer to the log file |
Copy Code
|
---|---|
static void screen_ControlkeySending(object sender, ControlKeySendingEventArgs args) { IScreen screen = (IScreen)sender; //Open a file and append the data entered on the screen string fileName = @"C:\users\" + Environment.UserName + @"\Documents\userInputLog.txt"; StreamWriter w = File.AppendText(fileName); w.WriteLine(userInput); //Add a divider to indicate the end of the screen and close the file w.WriteLine("..................................................................."); w.Close(); //Reset for the next screen userInput = " "; } |
The final code in the Program.cs file is shown below:
Log User Input |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Text; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.IbmHosts; using System.IO; namespace LogUserInput { class Program { //Use a string for a buffer static public string userInput = " "; static void screen_BeforeSendKeys(object sender, BeforeSendKeysEventArgs args) { //Append each key entered on the screen to the buffer userInput += args.Keys; } static void screen_BeforeSendControlKey(object sender, BeforeSendControlKeyEventArgs args) { IIbmScreen screen = (IIbmScreen)sender; string LogFile = @"C:\users\" + Environment.UserName + @"\Documents\userInputLog.txt"; //If the Transmit control key is sent to change the screen, write the user input in the buffer //to a log file if (args.Key.Equals(ControlKeyCode.Transmit) == true) { //Open a file and append the data entered on the screen StreamWriter w = File.AppendText(LogFile); w.WriteLine(userInput); //Add a divider to indicate the end of the screen w.WriteLine("..................................................................."); w.Close(); //Reset the buffer for the next screen userInput = " "; } //For the Tab control key and other control keys, create a new line in the buffer else { userInput += "\r\n"; } } static void Main(string[] args) { //Get the last activated instance of InfoConnect Application app = MyReflection.ActiveApplication; //Make sure InfoConnect is running if (app != null) { //Get the active view IFrame frame = (IFrame)app.GetObject("Frame"); IView view = (IView)frame.SelectedView; //Check for an Ibm terminal if (view.Control.ToString() == "Attachmate.Reflection.Emulation.IbmHosts.IbmTerminal") { IIbmTerminal terminal = (IIbmTerminal)view.Control; IIbmScreen screen = terminal.Screen; screen.BeforeSendControlKey += new BeforeSendControlKeyEventHandler(screen_BeforeSendControlKey); screen.BeforeSendKeys += new BeforeSendKeysEventHandler(screen_BeforeSendKeys); } } else { Console.WriteLine("Could not find an instance of InfoConnect"); } Console.ReadKey(); } } } |
Log user input |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.IO; using Attachmate.Reflection.UserInterface; using Attachmate.Reflection.Framework; using Attachmate.Reflection.Emulation.OpenSystems; namespace FileTransfer { class Program { //Use a string for a buffer static public string userInput = " "; static void screen_keysSent(object sender, KeysSentEventArgs args) { //Append each key entered on the screen to the buffer userInput += args.Key; } static void screen_ControlkeySending(object sender, ControlKeySendingEventArgs args) { IScreen screen = (IScreen)sender; //Open a file and append the data entered on the screen string fileName = @"C:\users\" + Environment.UserName + @"\Documents\userInputLog.txt"; StreamWriter w = File.AppendText(fileName); w.WriteLine(userInput); //Add a divider to indicate the end of the screen and close the file w.WriteLine("..................................................................."); w.Close(); //Reset for the next screen userInput = " "; } static void Main(string[] args) { //Get the last activated instance of InfoConnect Application app = MyReflection.ActiveApplication; //Make sure InfoConnect is running if (app != null) { IFrame frame = (IFrame)app.GetObject("Frame"); IView view = (IView)frame.SelectedView; if (view.Control.ToString() == "Attachmate.Reflection.Emulation.OpenSystems.Terminal") { ITerminal terminal = (ITerminal)view.Control; IScreen screen = terminal.Screen; screen.KeysSent += new KeysSentEventHandler(screen_keysSent); screen.ControlKeySending += new ControlKeySendingEventHandler(screen_ControlkeySending); } } else { Console.WriteLine("Cannot find an instance of InfoConnect"); } Console.ReadKey(); } } } |