Intercepting Keystrokes

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

Can anyone point me out how to intercept the keystrokes from a c#
application that's not a active window? - intercepting the system wide
keystrokes before it hits the target active window?

Thanks!

John
 
John:

I've done this in the past by installing a keyboard hook. The
keyboard hook does whatever it needs to do and either passes the
message on the the active application or simply swallows the keys.

Another way to do this is to tie the input queus of the 2 applications
together, but I've not done this w/ .NET assemblies --- only regular
Win32 processes.

I'd go w/ the keyboard hook.

John
 
Will the hook only work for the app that set it. .net hooks don't work
system wide do they?
 
Hi John,

Thank you for posting in the community!

Based on my understanding, you want to intercept the key stroke of another
application.

========================================
Normally, you can use hook to intercept key stroke in C#, for more
information, please refer to:
"HOW TO: Set a Windows Hook in Visual C# .NET"
http://support.microsoft.com/default.aspx?scid=kb;en-us;318804

This KB installs mouse hook, you can modify it and use keyboard hook.

But, as you want to intercept another application's key stroke, then a
global hook is needed. In the KB, you will see that:

"Global Hook Is Not Supported in .NET Framework

You cannot implement global hooks in Microsoft .NET Framework. To install a
global hook, a hook must have a native dynamic-link library (DLL) export to
inject itself in another process that requires a valid, consistent function
to call into. This requires a DLL export, which .NET Framework does not
support. Managed code has no concept of a consistent value for a function
pointer because these function pointers are proxies that are built
dynamically."

So you can not get this done in .Net. So you should do it in Win32
environment, please Klaus's suggestion to search in google, there will be a
lot of discussion about get this done in Win32 SDK.

=====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice weekend!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi John,

Thanks very much for your feedback.

I am glad my reply make sense to you. Actually, the Hook is a system
related technical, I will show you why global hook is not supported in .Net:

Global hook will inject a dll into another process's space, while the
managed dll will only executive in CLR runtime, so your managed dll will
not work in that process's memory space.

So, I think this support is not due to the .Net Framework, it is limited by
the design of the architecture.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi John,

Do you still have any concern? Please feel free to feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top