[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Giving my program the focus, always


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?

1 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
ba272Author Commented:
Thanks a lot.

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


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now