Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subclassing and hooks

Status
Not open for further replies.

dogdar

IS-IT--Management
May 15, 2002
3
0
0
US
I am writing a program that is a front end for a screen saver. It will communicate with a server, then start a preselected screen saver (a seperate & independent .scr file). I need to intercept certain keystrokes to prevent the screen saver from stopping (only certain keys, others are OK). I have been trying to use subclassing to accomplish this, but cannot get it to work, I think that hooks might be the way to go, but do not know how to implement it. Any suggestions on how to proceed? Thanks!

-Rick
 
This is just a rough example:

Public Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long

Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long

Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

Public Const WH_KEYBOARD = 2
Public Const KBH_MASK = &H20000000

Global hHook As Long

Public Function KeyboardProc(ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If nCode >= 0 Then
'Process keys you want to filter - here I am capturing the key "C"
If wParam = Asc(&quot;C&quot;) And (lParam And KBH_MASK) <> 0 Then
If (lParam And &HC0000000) = 0 Then
'Do Something
KeyboardProc = 1
Exit Function
End If
End If
End If
KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
End Function


Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD, _
AddressOf KeyboardProc, _
0&, _
App.ThreadID)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call UnhookWindowsHookEx(hHook)
End Sub
 
Thanks for the code - I will give it a try. One question though - in the SetWindowsHookEx call, I assume the threadID is for the app I am trying to hook into - in this case the screen saver. How do I get this info? Wouldn't App.ThreadID return the ID for my app, not the screen saver? I am starting the screen saver with a shell command which returns the taskID which I believe is different. When trying to use subclassing, I found a way to enumerate all windows and so was able to find the handle, but I assume that this is also incorrect. Thanks again for the help.
 
I see....You may need to create your own screen saver....
Or try a loop on the shell to keep it running.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top