Yes, it can. In the next link there is a demonstration of a system wide keyboard hook in VB.NET:
http://www.developer.com/net/net/print.php/11087_2193301_3
Below is a VB.NET global mouse hook class based on the code of the above link. It should be very simple to convert it to c.
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Imports TestMouseHook
Public Class Class1
Private f As New Form1()
Public Structure POINTAPI
Public x As Long
Public y As Long
End Structure
Public Structure MSLLHOOKSTRUCT
Public pt As POINTAPI
Public MouseData As Long
Public Flags As Long
Public Time As Long
Public dwExtraInfo As Long
End Structure
Constants
Public Const WM_RBUTTONDOWN = H204
Public Const WM_RBUTTONUP = H205
Private Const WH_MOUSE_LL = 14 Hook Flag
Private Const HC_ACTION = 0
Public mHandle As Integer
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal lpfn As MouseHookDelegate, ByVal hmod As Integer, _
ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As Integer, _
ByVal lParam As MSLLHOOKSTRUCT) As Integer
Public Function MouseCallback(ByVal Code As Integer, _
ByVal wParam As Integer, _
ByRef lParam As MSLLHOOKSTRUCT) As Integer
If (Code = HC_ACTION) Then
Debug.WriteLine(wParam.ToString())
Select Case wParam
Case WM_RBUTTONDOWN, WM_RBUTTONUP
Debug.WriteLine(wParam.ToString())
Return 1
End Select
End If
Return CallNextHookEx(mHandle, _
Code, wParam, lParam)
End Function
Public Delegate Function MouseHookDelegate( _
ByVal Code As Integer, _
ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) _
As Integer
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callback As MouseHookDelegate
Public Sub HookMouse(ByRef ff As Form)
f = ff
callback = New MouseHookDelegate(AddressOf MouseCallback)
mHandle = SetWindowsHookEx( _
WH_MOUSE_LL, callback, _
Marshal.GetHINSTANCE( _
Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub
Public Sub UnHookMouse()
Call UnhookWindowsHookEx(mHandle)
End Sub
End Class
"Dyeus"
--------------------------------------
>< note : because I am working in >c ... and looking for c code ... but
>this message discusses postings on the >vb.net group, I am posting to
>both c and vb.net groups >
>http://support.microsoft.com/default.as>px?scid=kbEN-USQ3188043
>
>gives an example in c of a mouse hook >specific to a thread, and states
>clearly :
>
>"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."
>
>I have interpreted this to mean that >when your application thread(s) are
>not active you have no way to have >your app receive notification of
>keyboard or mouse-events.
>
>Recently visualcore.com has posted >some vb.net source code that
>demonstrates that ... at least in >vb.net ... you can, indeed receive
>events when your application is >inactive. The following Google link >will
>take you to thread summaries of two >postings relevant to this issue.
>(source code example is provided on >one of those posts)
>
>http://groups.google.com/groups?>hl=enlr=ie=UTF-8oe=UTF8scorin
>g=dq=visualcore.com+hook
>
>I have mucked about with Jeremys code >and found that a simple
>modification will let you make the >vb.net app active, bring it to the
>front, give it the focus, etc.
>
>I tend to assume when I dont >understand something in c that its >my
>understanding that needs revision :) >So Id really appreciate any
>pointers to a deeper understanding of >what is and is not possible in c
>in regard to hooking events when the >c app doesnt have the focus.
>
>thanks, Bill Woodruff