Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi

 

Wondering if anyone knows how to accept keyboard input in C# without having to have a given form in focus.

 

I've found the KeyPress, Up and Down events and also the IMessageFilter interface to work however they cease to function when the form on which they are implmented loses focus.

 

I'm trying to detect Left Right and spacebar keystrokes and then raising button click events when the project's process is running no matter what has the focus.

 

Thanks

 

SEVI

Posted

sounds like a job for the api... i'd give you a good api resource or something but i don't have one at the moment.

 

get to searching :)

brandon

i'm not lazy i'm just resting before i get tired.
Posted
i dont know a way to use it in win32 but there are some good ways to get them using directinput, just a look in the SDK samples will show you how to solve that
Posted

Thanks all. Some of this stuff looks quite involved given the return it will provide and on second thoughts it may cause dramas as if the keystrokes are intercepted globally then it means if the keystroke was intended for the operating system then it won't get there.

 

I also looked at forcing the form to always have focus and then to just use the keypress event which is a round about way of doing the same thing. The odd thing is when I try to do this using Form.Activate events upon form_Deactivate the focus goes back to the form but keystrokes are not sent to the form until it is clicked on using the mouse. See the code below:

 

public class frmConsole : System.Windows.Forms.Form {

private System.ComponentModel.IContainer components;

 

public frmConsole() {

 

InitializeComponent();

}

 

protected override void Dispose( bool disposing ) {

if( disposing ) {

if (components != null) {

components.Dispose();

}

}

base.Dispose( disposing );

}

 

#region Windows Form Designer generated code

private void InitializeComponent()

{

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(264, 269);

this.Name = "frmConsole";

this.ShowInTaskbar = false;

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Console";

this.TopMost = true;

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmConsole_KeyDown);

this.Deactivate += new System.EventHandler(this.frmConsole_Deactivate);

 

}

#endregion

 

[sTAThread]

static void Main() {

Application.Run(new frmConsole());

}

 

private void frmConsole_Deactivate(object sender, System.EventArgs e) {

this.Activate();

}

 

private void frmConsole_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {

MessageBox.Show("Keystroke intercepted");

}

}

 

Is this as good as it gets or is Activate() the wrong method to use?

 

Thanks

Posted
it may cause dramas as if the keystrokes are intercepted globally then it means if the keystroke was intended for the operating system

 

i didnt look this much up but i think it is easily possible to check if the wanted form has focus/is active or not?

 

i think you should check that, which could be easier for applications where you dont have the source off and cant modify!

 

you can also activate a form and if you want a mouse click you even can emulate them, spy++ can also display events

 

you can even get the current size of window and translate that to screen coordinates if you want to activate by emulated mouse click

 

normal the "normal" activation of windows is easier then by doing it per mouse but it would be also a good training :D

 

hope that helps

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...