Detectar el Teclado en Visual

Estoy realizando una sencilla aplicación que consta de un cronómetro y un contador que cuenta el número de pulsaciones del teclado, pero mi pregunta es:¿Cómo detecto el numero de pulsaciones que se están produciendo sobre otra aplicación(Por Ejemplo al escribir en WORD) si Visual Solo detecta con el evento Keypress sobre el formulario activo?

1 respuesta

Respuesta
1
Lo tenes que hacer por medio de API,
declara esta funcion y estas constantes en un modulo
Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
Global v(44)
Global tx1
Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39
Public Const VK_A = &H41
Public Const VK_B = &H42
Public Const VK_C = &H43
Public Const VK_D = &H44
Public Const VK_E = &H45
Public Const VK_F = &H46
Public Const VK_G = &H47
Public Const VK_H = &H48
Public Const VK_I = &H49
Public Const VK_J = &H4A
Public Const VK_K = &H4B
Public Const VK_L = &H4C
Public Const VK_M = &H4D
Public Const VK_N = &H4E
Public Const VK_O = &H4F
Public Const VK_P = &H50
Public Const VK_Q = &H51
Public Const VK_R = &H52
Public Const VK_S = &H53
Public Const VK_T = &H54
Public Const VK_U = &H55
Public Const VK_V = &H56
Public Const VK_W = &H57
Public Const VK_X = &H58
Public Const VK_Y = &H59
Public Const VK_Z = &H5A
Public Const VK_SPACE = &H20
Public Const VK_EXECUTE = &H2B
ahora en un timer con su propiedad timer.Interval=1
pone:
Timer1.Timmer()
v(1) = GetAsyncKeyState(VK_0)
v(2) = GetAsyncKeyState(VK_1)
v(3) = GetAsyncKeyState(VK_2)
v(4) = GetAsyncKeyState(VK_3)
v(5) = GetAsyncKeyState(VK_4)
v(6) = GetAsyncKeyState(VK_5)
v(7) = GetAsyncKeyState(VK_6)
v(8) = GetAsyncKeyState(VK_7)
v(9) = GetAsyncKeyState(VK_8)
v(10) = GetAsyncKeyState(VK_9)
v(11) = GetAsyncKeyState(VK_A)
v(12) = GetAsyncKeyState(VK_B)
v(13) = GetAsyncKeyState(VK_C)
v(14) = GetAsyncKeyState(VK_D)
v(15) = GetAsyncKeyState(VK_E)
v(16) = GetAsyncKeyState(VK_F)
v(17) = GetAsyncKeyState(VK_G)
v(18) = GetAsyncKeyState(VK_H)
v(19) = GetAsyncKeyState(VK_I)
v(20) = GetAsyncKeyState(VK_J)
v(21) = GetAsyncKeyState(VK_K)
v(22) = GetAsyncKeyState(VK_L)
v(23) = GetAsyncKeyState(VK_M)
v(24) = GetAsyncKeyState(VK_N)
v(25) = GetAsyncKeyState(VK_O)
v(26) = GetAsyncKeyState(VK_P)
v(27) = GetAsyncKeyState(VK_Q)
v(28) = GetAsyncKeyState(VK_R)
v(29) = GetAsyncKeyState(VK_S)
v(30) = GetAsyncKeyState(VK_T)
v(31) = GetAsyncKeyState(VK_U)
v(32) = GetAsyncKeyState(VK_V)
v(33) = GetAsyncKeyState(VK_W)
v(34) = GetAsyncKeyState(VK_X)
v(35) = GetAsyncKeyState(VK_Y)
v(36) = GetAsyncKeyState(VK_Z)
v(36) = GetAsyncKeyState(VK_Z)
v(37) = GetAsyncKeyState(VK_SPACE)
v(38) = GetAsyncKeyState(VK_RETURN)
If (v(1) And &H1) = &H1 Then tx1 = tx1 + "0"
If (v(2) And &H1) = &H1 Then tx1 = tx1 + "1"
If (v(3) And &H1) = &H1 Then tx1 = tx1 + "2"
If (v(4) And &H1) = &H1 Then tx1 = tx1 + "3"
If (v(5) And &H1) = &H1 Then tx1 = tx1 + "4"
If (v(6) And &H1) = &H1 Then tx1 = tx1 + "5"
If (v(7) And &H1) = &H1 Then tx1 = tx1 + "6"
If (v(8) And &H1) = &H1 Then tx1 = tx1 + "7"
If (v(9) And &H1) = &H1 Then tx1 = tx1 + "8"
If (v(10) And &H1) = &H1 Then tx1 = tx1 + "9"
If (v(11) And &H1) = &H1 Then tx1 = tx1 + "a"
If (v(12) And &H1) = &H1 Then tx1 = tx1 + "b"
If (v(13) And &H1) = &H1 Then tx1 = tx1 + "c"
If (v(14) And &H1) = &H1 Then tx1 = tx1 + "d"
If (v(15) And &H1) = &H1 Then tx1 = tx1 + "e"
If (v(16) And &H1) = &H1 Then tx1 = tx1 + "f"
If (v(17) And &H1) = &H1 Then tx1 = tx1 + "g"
If (v(18) And &H1) = &H1 Then tx1 = tx1 + "h"
If (v(19) And &H1) = &H1 Then tx1 = tx1 + "i"
If (v(20) And &H1) = &H1 Then tx1 = tx1 + "j"
If (v(21) A
v(21) = GetAsyncKeyState(VK_K)
v(22) = GetAsyncKeyState(VK_L)
v(23) = GetAsyncKeyState(VK_M)
v(24) = GetAsyncKeyState(VK_N)
v(25) = GetAsyncKeyState(VK_O)
v(26) = GetAsyncKeyState(VK_P)
v(27) = GetAsyncKeyState(VK_Q)
v(28) = GetAsyncKeyState(VK_R)
v(29) = GetAsyncKeyState(VK_S)
v(30) = GetAsyncKeyState(VK_T)
v(31) = GetAsyncKeyState(VK_U)
v(32) = GetAsyncKeyState(VK_V)
v(33) = GetAsyncKeyState(VK_W)
v(34) = GetAsyncKeyState(VK_X)
v(35) = GetAsyncKeyState(VK_Y)
v(36) = GetAsyncKeyState(VK_Z)
v(36) = GetAsyncKeyState(VK_Z)
v(37) = GetAsyncKeyState(VK_SPACE)
v(38) = GetAsyncKeyState(VK_RETURN)
If (v(1) And &H1) = &H1 Then tx1 = tx1 + "0"
If (v(2) And &H1) = &H1 Then tx1 = tx1 + "1"
If (v(3) And &H1) = &H1 Then tx1 = tx1 + "2"
If (v(4) And &H1) = &H1 Then tx1 = tx1 + "3"
If (v(5) And &H1) = &H1 Then tx1 = tx1 + "4"
If (v(6) And &H1) = &H1 Then tx1 = tx1 + "5"
If (v(7) And &H1) = &H1 Then tx1 = tx1 + "6"
If (v(8) And &H1) = &H1 Then tx1 = tx1 + "7"
If (v(9) And &H1) = &H1 Then tx1 = tx1 + "8"
If (v(10) And &H1) = &H1 Then tx1 = tx1 + "9"
If (v(11) And &H1) = &H1 Then tx1 = tx1 + "a"
If (v(12) And &H1) = &H1 Then tx1 = tx1 + "b"
If (v(13) And &H1) = &H1 Then tx1 = tx1 + "c"
If (v(14) And &H1) = &H1 Then tx1 = tx1 + "d"
If (v(15) And &H1) = &H1 Then tx1 = tx1 + "e"
If (v(16) And &H1) = &H1 Then tx1 = tx1 + "f"
If (v(17) And &H1) = &H1 Then tx1 = tx1 + "g"
If (v(18) And &H1) = &H1 Then tx1 = tx1 + "h"
If (v(19) And &H1) = &H1 Then tx1 = tx1 + "i"
If (v(20) And &H1) = &H1 Then tx1 = tx1 + "j"
If (v(21) And &H1) = &H1 Then tx1 = tx1 + "k"
If (v(22) And &H1) = &H1 Then tx1 = tx1 + "l"
If (v(23) And &H1) = &H1 Then tx1 = tx1 + "m"
If (v(24) And &H1) = &H1 Then tx1 = tx1 + "n"
If (v(25) And &H1) = &H1 Then tx1 = tx1 + "o"
If (v(26) And &H1) = &H1 Then tx1 = tx1 + "p"
If (v(27) And &H1) = &H1 Then tx1 = tx1 + "q"
If (v(28) And &H1) = &H1 Then tx1 = tx1 + "r"
If (v(29) And &H1) = &H1 Then tx1 = tx1 + "s"
If (v(30) And &H1) = &H1 Then tx1 = tx1 + "t"
If (v(31) And &H1) = &H1 Then tx1 = tx1 + "u"
If (v(32) And &H1) = &H1 Then tx1 = tx1 + "v"
If (v(33) And &H1) = &H1 Then tx1 = tx1 + "w"
If (v(34) And &H1) = &H1 Then tx1 = tx1 + "x"
If (v(35) And &H1) = &H1 Then tx1 = tx1 + "y"
If (v(36) And &H1) = &H1 Then tx1 = tx1 + "z"
If (v(37) And &H1) = &H1 Then tx1 = tx1 + " "
If (v(38) And &H1) = &H1 Then tx1 = tx1 + Chr(13) + Chr(10)
End Sub
Ojo que corte mal las secciones de arriba, vas a tener lineas duplicadas, borralas por favor y disculpame.
Fijate que resumi la lista de teclas para que entre en este mansaje, estan solo las letras, los numeros y las tecla de espacio y enter. El resto tambien se puede agregar.
La rutina del Timer1 te almacena las teclas persionadas en la variable tx1, vos podes con otro timer poner esto:
este timer con Timer2.Interval=1000
Timer2.Timmer()
label1 = len(tx1)
tx1 = ""
End Sub
Con esto en label1 te va a poner la cantidad de teclas presionadas por segundo.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas