Reflection .NET API
Walkthrough / Log User Input and Credit Card Access / Log User Input
In This Topic
    Log User Input
    In This Topic

    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 Reflection.

    To log keys entered by users

    1. In Visual Studio, create a new Console Application project and add references for the following Reflection assemblies. (Depending on your version of Visual Studio, these can be found either on the .NET tab or under Assemblies | Extensions.)
      Attachmate.Reflection
      Attachmate.Reflection.Framework
      Attachmate.Reflection.Emulation.IbmHosts (if you're using IBM)
      Attachmate.Reflection.Emulation.OpenSystems (if you're using UNIX or OpenVMS)
    2. Replace all the code in the Program.cs file with the following code for the terminal you are using. This code gets a handle to Reflection if it is running. If the type of terminal you want to monitor is selected in the workspace, it creates event handlers for two events (BeforeSendKeys and BeforeSendControlKeys). We'll add the code for these event handlers next.
      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 Reflection           
                  Application app = MyReflection.ActiveApplication;
      
                  //Make sure Reflection 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 Reflection");
                  }
                  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 Reflection
                  Application app = MyReflection.ActiveApplication;
      
                  //Make sure Reflection 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 Reflection");
                  }
                  Console.ReadKey();
              }
          }
      }
      
    3. Add the Screen_BeforeSendKeys event handler along with the userInput string to the Program class. As each key is entered, this event handler adds it to the userInput string, which acts as a buffer that holds the content entered for the screen. 
      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;
              }
      
    4. Add the BeforeSendControlKey event handler to the Program class. When a control key is entered, this code opens a log file and appends the contents of the userInput string buffer into the file.
      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 = " ";
              }
      

     

    To test this project 

    1. Open Reflection and then open a session document file.
    2. In Visual Studio, press F5 to run the project.
    3. Enter some data and navigate to a few different screens.
    4. Open the userInputLog.txt log file in your Documents folder and verify that the data you entered was logged in the file.

    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 Reflection           
                Application app = MyReflection.ActiveApplication;
    
                //Make sure Reflection 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 Reflection");
                }
                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 Reflection
                Application app = MyReflection.ActiveApplication;
    
                //Make sure Reflection 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 Reflection");
                }
                Console.ReadKey();
            }
        }
    }