Windos hooking and messages

  • Thread starter Thread starter Ofry
  • Start date Start date
O

Ofry

Hi,
I'm writing a c# application and I would like to know every time the input
language changes.
I have this code:
InputLanguage myCurrentLanguage = InputLanguage.CurrentInputLanguage;
String lang = myCurrentLanguage.Culture.TwoLetterISOLanguageName;
That gives me the current language, but I want to be notyfied whenever it
changes and not check every few seconds.

I've found a windows message WM_INPUTLANGUAGECHANGE, where do I need to hook
to in order to get this message?
Is there a better way? maybe another event I'm not aware of

Thanks,
Ofry
 
Thanks, but this event rises only on input language change in my form, I want
to know every time the user changes his language, even if it is in another
form / window / process.
 
[...]
I've found a windows message WM_INPUTLANGUAGECHANGE, where do I need to
hook
to in order to get this message?
Is there a better way? maybe another event I'm not aware of

I'm not aware of a built-in .NET event that would pass along that
message. However, you can always override the WndProc() method in your
Control-inheriting class to catch window messages sent to the control or
form.

There is also the IMessageFilter interface, but it's really intended for a
different purpose and is probably overkill for this goal.

Pete
 
Hi Peter, thanks,
to use the WndProc(), I have to have a running form right? But I want to
listen from a process that has no open form.




Peter Duniho said:
[...]
I've found a windows message WM_INPUTLANGUAGECHANGE, where do I need to
hook
to in order to get this message?
Is there a better way? maybe another event I'm not aware of

I'm not aware of a built-in .NET event that would pass along that
message. However, you can always override the WndProc() method in your
Control-inheriting class to catch window messages sent to the control or
form.

There is also the IMessageFilter interface, but it's really intended for a
different purpose and is probably overkill for this goal.

Pete
 
Hi Peter, thanks,
to use the WndProc(), I have to have a running form right? But I want to
listen from a process that has no open form.

Sorry. I didn't see anything in your original message that said that.

You may have to go with p/invoke. I'm not really sure. I don't know of a
managed way to hook window messages generally.

If there's a way to do it in managed code, there are at least a couple of
other people who read this newsgroup that are more familiar with such
things and would know. Hopefully they'll see this thread and reply.

Pete
 
Ofry said:
Thanks, but this event rises only on input language change in my form, I
want
to know every time the user changes his language, even if it is in another
form / window / process.

Don't know what you mean by this, the "InputLanguageChanged" event is raised
when the *user* changes the input language at the system level, all windows
application running in the users login session will get this event (a
windows message WM_INPUTLANGCHANGE) when the user changes the input
language.

Consider following sample:

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

public class Form1 : System.Windows.Forms.Form
{
Label lbl = new Label();
public Form1()
{
this.Controls.Add(lbl);
lbl.Size = new Size(400, 48);
lbl.Padding = new Padding(5);
// uncomment if you want to handle the event in the
InputLanguageChangedEventHandler
//this.InputLanguageChanged += new
InputLanguageChangedEventHandler(languageChange);
}
private void languageChange(Object sender, InputLanguageChangedEventArgs
e)
{
lbl.Text = e.InputLanguage.Culture.ToString();
}
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x0051) // WM_INPUTLANGCHANGE
{
lbl.Text = string.Format("Posting Thread: {0} \n{1}",
Thread.CurrentThread.ManagedThreadId, m);
}
base.WndProc(ref m);
}
public static void Main(string[] args)
{
Application.Run(new Form1());
}
}

This illustrates how you can add your own handler by overriding WndProc, or
using the predefined handler for this message, however, both handle the same
message!.

Willy.
 
Back
Top