Newsgroup: it.comp.lang.visual-basic
Titolo: Suggerimento per chi desidera realizzare il P1 (Hook tastiera)

Rovistando nella MSDN library ho trovato alcuni suggerimenti per
intercettare gli eventi della tastiera.
Questo stesso testo dovrebbe essere repelibile sul sito microsoft
nella KB come articlolo Q177992 .
Lo ripubblico qui per quelli che ne fossero interessati. Ciao.
_____________________________________________________
The next section illustrates how to create a sample application that
works around this behavior by intercepting the access key, setting the
focus, and then causing a mouse-click event to occur. Please note that
this does not resolve all issues with the key order problems. For
example, calling Message boxes from an event affects the event order
as well. 
WARNING: Failure to unhook the keyboard hook before exiting your
application will result in errors, Invalid Page Faults, and data loss.
This is caused by the system pointing to the your KeyboardProc()
function, which no longer exists because it has not been notified of
the change. Always unhook the keyboard upon exiting the application.
This is especially important while debugging a program that uses this
technique within the Microsoft Visual Basic 5.0 Development
Environment. Clicking the End button or selecting End from the Run
menu without unhooking will cause an Invalid Page Fault and close
Microsoft Visual Basic without giving you a chance to save your code. 
Steps to Create Sample Project 
1.Start a new Standard EXE project in Visual Basic. Form1 is created
by default. 
2.Add a TextBox and a CommandButton to Form1. Set the TabIndex of the
TextBox to 0. 
3.Add a module to the project. From the Project menu, click Add
Module. 
4.Copy the following code to the Code window of Module1: 
      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
      Public Const WM_LBUTTONDOWN = &H201
      Public Const WM_LBUTTONUP = &H202
      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
            If wParam = Asc("C") And (lParam And KBH_MASK) <> 0 Then
               If (lParam And &HC0000000) = 0 Then
                  Form1.Command1.SetFocus
                     Call PostMessage(Form1.Command1.hwnd, _
                                       WM_LBUTTONDOWN, _
                                       0, _
                                       &H20002)
                     Call PostMessage(Form1.Command1.hwnd, _
                                       WM_LBUTTONUP, _
                                       0, _
                                       &H20002)
                     KeyboardProc = 1
                     Exit Function
               End If
            End If
         End If
         KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
      End Function
5.Copy the following code to the Code window of the Form1 form: 
      Option Explicit
      Private Sub Command1_Click()
         Debug.Print "Command1_Click"
      End Sub
      Private Sub Command1_GotFocus()
         Debug.Print "Command1_GotFocus"
      End Sub
      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
      Private Sub Text1_LostFocus()
         Debug.Print "Text1_LostFocus"
      End Sub
6.Press the F5 key to run the program. 
The text box should have the focus. Click the CommandButton and note
the sequence of events that occur as show in the debug window: 
      Text1_LostFocus
      Command1_GotFocus
      Command1_Click
Set the focus to the text box and press the ALT+C keys and note that
the same events occur. 
Notes
Hooks do not always behave the same way in the IDE as they do in an
EXE. Make certain that you test your solution in an EXE before you
move on to other parts of your project. 
It is possible to intercept all of the keyboard input on a system, but
not using "pure" Visual Basic. The hook must be placed in a standard
DLL; while Visual Basic 5.0 can create OLE DLLs, it cannot create
standard DLLs. 
(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by
Arsenio Locsin, Microsoft Corporation 

 
Ok, non e' esattamente quello che si intendeva, ma e' sicuramente MOLTO interessante.
Ne ho fatto un progettino che sto per mettere sul Sito Comune.
Quello che sarebbe bello avare, e' una OCX/DLL che intercetti TUTTI gli eventi di tastiera che
avvengono da parte dellutente, anche quelli dirette ad altre applicazioni che non e' quella che
ha installato l'HOOK.
Grazie per l'ottima collaborazione.
-- 
Hi, Ciao
Ettore                 By -MES-
http://freeweb.aspide.it/freeweb/ilmes
Il sito comune di programmatori VB
http://members.xoom.com/it_lang_vb


prego 
Ciao


 
Infatti l'MSDN parla chiaro, occorre fare una DLL.
In effetti la DLL andrebbe fatta in C o in Delphi.
Per far arrivare i messaggi anche all'applicazione VB penso che una delle poche strade
percorribili sia con il PostMessage() ( o andrebbe bene anche il SendMessage ?).
