View Full Version : [VB6] intercettare tasti
Diabolik88m
23-08-2006, 22:40
ciao a tutti tramite l'api "GetAsyncKeyState" riesco a intercettare i tasti che vengono premuti anche quando la mia applicazione non ha il focus, ora io vorrei sapere come si fa a cambiare i tasti che l'utente preme: per esempio io vorrei fare in modo che se l'utente preme il tasto "A" il computer invece scriva "B".
Il codice che uso per intercettare i tasti è il seguente:
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyA) & H8000 Then
'qua non so che codice scrivere per far si che il computer scriva "B" invece che "A"
End If
End Sub
spero che qualcuno mi possa aiutare.... va bene anche se postate un codice completamente diverso dala bozza che ho scritto io :D
ciao :)
se ho capito bene vuoi che ogni lettera sia seguita dalla consecutiva nell'alfabeto, quindi puoi fare cosi
if vbkeyA != "Z" AND vbkeyA != "z" then
vbkeyA++;
endif 'non conosco la sintassi dell'else :(
if vbkeyA == "Z" then
vbkeyA = "A"
endif
if vbkeyA == "z" then
vbkeyA = "a"
endif
se invece hai altri criteri per sostituire le lettere (es s -> d, f->r ...)
devi fare un ifelse con tutte le lettere:
if vbkeyA=="A" OR vbkeyA=="a" then
vbkeyA=="*" 'al posto dell'asterisco metti la lettera che rimpiazzera la A
endif
if vbkeyA=="B" ' ecc. ecc. (ovviamente tu fallo con l'else)
Diabolik88m
24-08-2006, 10:28
AngeL) grazie per il codice ma io volevo intercettare i tasti che vengono premuti anche quando la mia applicazione nn ha il focus oppure sta lavorando in background.... io uso VB6, il codice che hai posto te invece credo sia scritto in Java o un linguaggio simile, non so come funzioni nel Java o quello che è, ma in VB6 devo per forza ricorrere a un'Api per intercettare i tasti premuti a livello di sistema :p ...
cmq grazie mille lo stesso :D ...
Allora devi usare un hook di sistema, ma il problema è un hook di sistema deve essere installato da una DLL...e come si dice qui:
Before you get excited, though, VB on its own cannot be used to create a system-wide hook. This is because the hook procedure must reside within a Windows DLL, and VB cannot create these beasts (because you cannot specify to export the HookProc function). Also I should point out that system-wide hooks aren't much fun to write: you can't debug them very easily, and if anything goes wrong it takes your whole system down! If you have some C/C++ knowledge, however, there are various samples of creating system-wide hooks at MSDN and CodeGuru.
Questo è un esempio di hook che riguarda il thread stesso che ha settato l'hook:
'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
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
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'if idHook is less than zero, no further processing is required
If idHook < 0 Then
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
Else
'check if SHIFT-S is pressed
If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
'show the result
Form1.Print "Shift-S pressed ..."
End If
'call the next hook
KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
End If
End Function
'In a form, called Form1
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'set a keyboard hook
hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'remove the windows-hook
UnhookWindowsHookEx hHook
End Sub
tramite l'api "GetAsyncKeyState" riesco a intercettare i tasti che vengono premuti anche quando la mia applicazione non ha il focus
avevo pensato che tu fossi gia riuscito a intercettare i tasti...
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.