Giving my program the focus, always

Posted on 2005-04-23
Last Modified: 2010-04-16

I have written a keyboard logging program as part of my system to access the data being typed into a DOS system.  The program runs on a Toshiba laptop and gets the keytrokes via its PS/2 port, and is coming in from a KVM switch, which splits the singal from the DOS keyboard and sends the same keystrokes to both the DOS machine and the laptop.  But my worry is about the Window's focus.

In order for my system to work correctly, my key logger must always have the focus, which should not be a problem since the sole use for the laptop is to sniff the keyboard.  But my fear is that some pop-up program, whether it's from my anti-virus program or Windows itself, will move the focus and thereby cause my system to lose the keyboard stream.

Is there a way to have Windows always giving my program the focus?  I suppose I could have my program coninutally giving itself the focus.  Is there a way for Windows to do this?

Question by:ba272
    LVL 8

    Accepted Solution

    You can use mouse click automatic to focus this form:

    using System.Runtime.InteropServices;

                private System.Windows.Forms.Timer timer1;

                private static extern void mouse_event(
                      UInt32 dwFlags, // motion and click options
                      UInt32 dx, // horizontal position or change
                      UInt32 dy, // vertical position or change
                      UInt32 dwData, // wheel movement
                      IntPtr dwExtraInfo // application-defined information

                private void Form1_Load(object sender, System.EventArgs e)
                      this.timer1 = new System.Windows.Forms.Timer(this.components);
                      // timer1
                      this.timer1.Enabled = true;
                      this.timer1.Interval = 1000;
                      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);            

                private void timer1_Tick(object sender, System.EventArgs e)
                      Point location = Cursor.Position; // store old pos
                      Cursor.Position = new System.Drawing.Point(this.Left + 30,this.Top + 30);// point to form
                      mouse_event(2, 0, 0, 0, new System.IntPtr()); //click
                      Cursor.Position = location; // goto old pos

    Good luck

    Author Comment

    Thanks a lot.

    I like the idea of simulating a mouse click - could come in handy elsewhere also.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now