Test Foro de elhacker.net SMF 2.1

Programación => .NET (C#, VB.NET, ASP) => Programación General => Programación Visual Basic => Mensaje iniciado por: 50l3r en 26 Enero 2010, 23:53 PM

Título: Detectar atajos de teclado
Publicado por: 50l3r en 26 Enero 2010, 23:53 PM
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
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 27 Enero 2010, 00:19 AM
weno, me autorespondo

http://www.recursosvisualbasic.com.ar/htm/trucos-codigofuente-visual-basic/10.htm

gracias ^^ xD
Título: Re: Detectar atajos de teclado
Publicado por: seba123neo en 27 Enero 2010, 22:53 PM
es mejor con la api RegisterHotKey, ya que eso solamente funciona si tu aplicación esta activa.
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 27 Enero 2010, 23:04 PM
pues lo iva a preguntar ahora mismo, me di cuenta cuando lo acabe que solo recibe la accion si tiene el foco
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 31 Enero 2010, 03:58 AM
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
Título: Re: Detectar atajos de teclado
Publicado por: seba123neo en 31 Enero 2010, 04:22 AM
con un Or vas a agregando mas...por ejemplo CTRL + ALT + 1

Código (vb) [Seleccionar]
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.





Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 31 Enero 2010, 13:01 PM
ok muchas gracias

mira tengo otra duda, el proceso para ello es el sigueinte

Código (vb) [Seleccionar]
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:

Código (vb) [Seleccionar]
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
Título: Re: Detectar atajos de teclado
Publicado por: raul338 en 31 Enero 2010, 20:00 PM
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)
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 31 Enero 2010, 22:01 PM
no te entendi muy bien
Título: Re: Detectar atajos de teclado
Publicado por: Karcrack en 1 Febrero 2010, 00:06 AM
http://msdn.microsoft.com/en-us/library/bb775233(VS.85).aspx
:-*
Título: Re: Detectar atajos de teclado
Publicado por: seba123neo en 1 Febrero 2010, 02:05 AM
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.
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 1 Febrero 2010, 08:56 AM
ok gracias sea123neo, ahora si lo entendi
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 1 Febrero 2010, 16:49 PM
esto en el form load:

Código (vb) [Seleccionar]
    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:
Código (vb) [Seleccionar]
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

Título: Re: Detectar atajos de teclado
Publicado por: Karcrack en 1 Febrero 2010, 17:23 PM
Utiliza un Select Case envez de tantos Ifs :rolleyes: :rolleyes:
Título: Re: Detectar atajos de teclado
Publicado por: 50l3r en 1 Febrero 2010, 18:16 PM
solo lo puse para ver si funcionaba, no funciona

al iniciar el proyecto se crea un bucle que dice" apretaste1"
Título: Re: Detectar atajos de teclado
Publicado por: raul338 en 1 Febrero 2010, 23:22 PM
es porque no pussite flitro. o sea, todos esos IF tenes que meterlos dentro de un if que valide si Msg es WM_HOTKEY

Código (vb) [Seleccionar]

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)