windows service with keybd_event is not printing the value in active window

Mar 26 2008 5:02 AM
 
Hello,

I have created a windows service in c#. I would like to use windows service and print the "x" value in the active window wherever the cursor located. I have used Keybd_event to print the value. Unfortunately i am unable to see the value in the active window. I assume Keybd_event is not fired. I have added log messages before and after keybd_event event, log messages are creating perfectly. However if i use the same code to print the value from an windows application i am able to print the value. I am attaching my code whatever i have used to print the value

Here is the code i have used in windows service and application

using System;
using System.Collections.Generic;
using System.Text;

namespace SmartcardLibrary
{
   /*
    * ---------------------------------------------------------------------------------------
    * Class Name   :   PrintToScreen
    * Purpose      :   Normal class inherits the keyboardevents abstract class and used to
    *                  get the correct keyborad character and print it on active window
    * ---------------------------------------------------------------------------------------
    * Orig Author  :   JAGANMOHAN NAIDU PENIKALAPATI
    * Created Date :   March 24 2008
    * ---------------------------------------------------------------------------------------
    * Updated Author   :
    * Updated Date     :
    * ---------------------------------------------------------------------------------------
    * */
   
   public class PrintToScreen : KeyboardEvents
   {
       private static log4net.ILog logError = log4net.LogManager.GetLogger("SmartCardErrorLogger");
       private const string NEW_LINE = "\n";

       private const uint KEYEVENTFKEYUP = 0x2;
       private const byte VKCAPSLOCK = 0x14;
       private const byte SCCAPSLOCK = 0xBA;
       private const byte VKTAB = 0x09;
       private const byte SCTAB = 0x8F;

       public static void Print(string input)
       {
           try
           {
               //logError.Error("PRINT SCREEN TAG ID: " + input + NEW_LINE);
               input = input.ToUpper();
               byte[] virtualKey = Encoding.UTF8.GetBytes(input);
               byte[] bScanCode = new byte[input.Length];
               for (int count = 0; count < virtualKey.Length; count++)
               {
                   bScanCode[count] = ScanCode(virtualKey[count]);
               }
               for (int i = 0; i < virtualKey.Length; i++)
               {
                   if (GetKeyState(VKCAPSLOCK) == 0)
                       keybd_event(VKCAPSLOCK, SCCAPSLOCK, 0, 0); //Caps Lock Press Event
                  keybd_event(virtualKey[i], bScanCode[i], 0, 0); //Key press event  
                   //UnsafeNativeMethods.keybd_event(virtualKey[i], bScanCode[i], KEYEVENTF_KEYUP, 0); //Key release event
                   //UnsafeNativeMethods.keybd_event(VKCAPSLOCK, SCCAPSLOCK, KEYEVENTFKEYUP, 0); //Caps Lock release Event
               }
                   keybd_event(VKTAB, SCTAB, 0, 0); //Tab press event
                   //logError.Error(input + NEW_LINE + NEW_LINE);
           }
           catch (Exception ex)
           {
               logError.Error(ex.Message + NEW_LINE + ex.StackTrace);
           }
       }

       private static byte ScanCode(byte Character)
       {
           byte result = Convert.ToByte(MapVirtualKey(Character, 0x1)); //0x1 indicates VirtualCode to Scan code conversion
           return result;
       }

   }///
}///

This code is working for application but not working for windows service...


your suggestions on this issue would be appreciated

Answers (1)