Muy buneas, quisiera saber como detectar atajos de teclado, es decir como saber en mi programa si se ha pulsado alt + f4 por ejemplo
alguien sabe con que funcion se puede hacer? intente con la api getasynckeystate pero me lo detecta unicamente cuando las pulso a la vez, no cuando dejo pulsada,una y despues otra, nose si me entendi bien
saludos
weno, me autorespondo
http://www.recursosvisualbasic.com.ar/htm/trucos-codigofuente-visual-basic/10.htm
gracias ^^ xD
es mejor con la api RegisterHotKey, ya que eso solamente funciona si tu aplicación esta activa.
pues lo iva a preguntar ahora mismo, me di cuenta cuando lo acabe que solo recibe la accion si tiene el foco
tengo una duda en esto ultimo:
RegisterHotKey(hWnd, 1, MOD_CONTROL, VK_V)
ahi apretando control + v genera el evento
pero si yo quisiera pasarle 3 teclas para un atajo de teclado, por ejemplo control + v + 1
como hago eso?
saludos
con un Or vas a agregando mas...por ejemplo CTRL + ALT + 1
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_NUMPAD1) = 0 Then
pero fijate que el tercer parametro no podria recibir la tecla "V", mira la documentacion de la api, ahi te explica solo cual pueden ser.
http://msdn.microsoft.com/en-us/library/ms646309%28VS.85%29.aspx (http://msdn.microsoft.com/en-us/library/ms646309%28VS.85%29.aspx)
saludos.
ok muchas gracias
mira tengo otra duda, el proceso para ello es el sigueinte
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_1) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_2) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_3) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_4) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_5) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_6) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_7) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_8) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
WinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
y en el modulo es asi:
Public Function NewWindowProc( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If Msg = &H82 Then
Call SetWindowLong(hWnd, GWL_WNDPROC, WinProc)
Call UnregisterHotKey(hWnd, 1)
End If
If Msg = WM_HOTKEY Then
MsgBox "atajos dados"
End If
NewWindowProc = CallWindowProc(WinProc, hWnd, Msg, wParam, lParam)
End Function
como hago para identificar si se ha dado a ctrl + alt + 2 en vez de a ctrl + alt + 7 por ejemplo? ya que para identificarlo usan la constante msg y devuelve unicamente el valor de que se ha ejecutado la pulsacion de teclas, una cualquiera
saludos
En el segundo parametro de RegisterHotKey deben ser todos numeros distintos (imagina, es como un handle del Hotkey) y en el sub NewWindowProc en lparam (o wparam si no me equivoco) devuelve el identificador del hotkey presionado (o sea, el segundo parametro del RegisterHotKey)
no te entendi muy bien
http://msdn.microsoft.com/en-us/library/bb775233(VS.85).aspx
:-*
cada registrada de combinaciones tiene un ID, es el segundo parametro, vos ahi estas registrando todas con el mismo ID = 1 , y cuando repetis los ID, solo te toma la ultima ...debes hacer algo como:
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_1) = 0 Then
...codigo...
If RegisterHotKey(hWnd, 2, MOD_CONTROL Or MOD_ALT, VK_1) = 0 Then
codigo...
If RegisterHotKey(hWnd, 3, MOD_CONTROL Or MOD_ALT, VK_1) = 0 Then
codigo....
y en el NewWindowProc el parametro wParam, te va a llegar con el ID de la combinacion que apretaste, y bueno ahi haces un if o un select case y listo...
PD: para desregistrar tambien debes hacerlo con el ID...
saludos.
ok gracias sea123neo, ahora si lo entendi
esto en el form load:
If RegisterHotKey(hWnd, 1, MOD_CONTROL Or MOD_ALT, VK_1) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 2, MOD_CONTROL Or MOD_ALT, VK_2) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 3, MOD_CONTROL Or MOD_ALT, VK_3) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 4, MOD_CONTROL Or MOD_ALT, VK_4) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 5, MOD_CONTROL Or MOD_ALT, VK_5) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 6, MOD_CONTROL Or MOD_ALT, VK_6) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 7, MOD_CONTROL Or MOD_ALT, VK_7) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
If RegisterHotKey(hWnd, 8, MOD_CONTROL Or MOD_ALT, VK_8) = 0 Then
MsgBox " Hubo un error ", vbCritical
Exit Sub
End If
WinProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf NewWindowProc)
esto en el modulo:
Public Function NewWindowProc( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If Msg = &H82 Then
Call SetWindowLong(hWnd, GWL_WNDPROC, WinProc)
Call UnregisterHotKey(hWnd, 1)
End If
if wparam = 1 then msgbox "apretaste 1"
if wparam = 2 then msgbox "apretaste 2"
if wparam = 3 then msgbox "apretaste 3"
if wparam = 4 then msgbox "apretaste 4"
if wparam = 5 then msgbox "apretaste 5"
if wparam = 6 then msgbox "apretaste 6"
if wparam = 7 then msgbox "apretaste 7"
if wparam = 8 then msgbox "apretaste 8"
NewWindowProc = CallWindowProc(WinProc, hWnd, Msg, wParam, lParam)
End Function
Utiliza un Select Case envez de tantos Ifs :rolleyes: :rolleyes:
solo lo puse para ver si funcionaba, no funciona
al iniciar el proyecto se crea un bucle que dice" apretaste1"
es porque no pussite flitro. o sea, todos esos IF tenes que meterlos dentro de un if que valide si Msg es WM_HOTKEY
Public Function NewWindowProc( _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If Msg = &H82 Then
Call SetWindowLong(hWnd, GWL_WNDPROC, WinProc)
Call UnregisterHotKey(hWnd, 1)
End If
if Msg = WM_HOTKEY Then ' Creo que es &H83 si mi memoria no me falla :P
if wparam = 1 then msgbox "apretaste 1"
if wparam = 2 then msgbox "apretaste 2"
if wparam = 3 then msgbox "apretaste 3"
if wparam = 4 then msgbox "apretaste 4"
if wparam = 5 then msgbox "apretaste 5"
if wparam = 6 then msgbox "apretaste 6"
if wparam = 7 then msgbox "apretaste 7"
if wparam = 8 then msgbox "apretaste 8"
end If
NewWindowProc = CallWindowProc(WinProc, hWnd, Msg, wParam, lParam)
End Function
PD: La proxima sere un poco mas claro para que me entiendas mejor :P (x la explicacion de antes :xD)