elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco  (Leído 7,690 veces)
PabloPbl

Desconectado Desconectado

Mensajes: 188


Ver Perfil
[Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« en: 17 Marzo 2015, 02:28 am »

Lo que quiero lograr es que aplicación se maximice al tocar una combinación de teclas.

Investigando por la web vi que decían que no es posible y que Java es un poco limitado en estas cosas que se hacen directamente con el SO y que se debía hacer uso de librerías de C/C++ pero no dieron mas información.

Se algo de C y C++ gracias a mis conocimientos de Java, pero muy básico.

¿Me podrían orientar un poco como hacerlo?

Y otra cosa ¿Como es posible hacer librerías con C y C++ para poder usarlas en Java?


En línea

ThunderCls


Desconectado Desconectado

Mensajes: 455


Coder | Reverser | Gamer


Ver Perfil WWW
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #1 en: 17 Marzo 2015, 03:24 am »

Me parece q lo q buscas son hotkeys globales, bueno pues en windows tienes esto:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx
saludos


En línea

-[ "…I can only show you the door. You're the one that has to walk through it." – Morpheus (The Matrix) ]-
http://reversec0de.wordpress.com
https://github.com/ThunderCls/
PabloPbl

Desconectado Desconectado

Mensajes: 188


Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #2 en: 17 Marzo 2015, 04:01 am »

Muchas gracias por el aporte. Si bien estoy viendo, eso es un código hecho en C++ y si no me equivoco tiene que ver con el uso de eventos de teclado, pero la duda viene ahora:
¿Como hago para conectar ese código para usarlo en Java?
En línea

Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.788



Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #3 en: 17 Marzo 2015, 06:19 am »

Muchas gracias por el aporte. Si bien estoy viendo, eso es un código hecho en C++ y si no me equivoco tiene que ver con el uso de eventos de teclado, pero la duda viene ahora:

Lo que te ha mostrado el compañero @[ Thunder | CLS ] es la referencia de la función RegisterHotKey de la API de Windows, arriba de la página te muestra la firma de la función y abajo un ejemplo de uso en C++.

Nota: Y la función UnregisterHotKey realiza lo opuesto.


Y otra cosa ¿Como es posible hacer librerías con C y C++ para poder usarlas en Java?

Antes de que sigas leyendo esto, te aconsejo que esperes nuevas respuestas al tema para que puedas hacerlo directamente en Java, te costará mucho menos esfuerzo,
sigue leyendo lo que te propongo solamente si te interesa experimentar técnicas alternativas, aunque ya te aviso que esto resultaría mucho más complejo, si no te apetece puedes omitir toda esta parrafada que he escrito abajo :).


...

Doy por hecho que no te importaría utilizar librerías de terceros en tu proyecto de Java, así que te presento un código que desarrollé hace tiempo para administrar los atajos globales del teclado (o system-wide hotkeys), el código está escrito en VB.Net, antes de seguir explicando te muestro un ejemplo de uso en puro código .Net:

Código
  1. Public Class Form1
  2.  
  3.    ''' <summary>
  4.    ''' Define the system-wide hotkey object.
  5.    ''' </summary>
  6.    Private WithEvents hotkey As HotkeyManager
  7.  
  8.    ''' <summary>
  9.    ''' Initializes a new instance of this class.
  10.    ''' </summary>
  11.    Public Sub Test() Handles MyBase.Shown
  12.  
  13.        MyClass.InitializeComponent()
  14.  
  15.        ' Registers a new global hotkey on the system. (Alt + Ctrl + A)
  16.        hotkey = New HotkeyManager(HotkeyManager.KeyModifier.Alt Or HotkeyManager.KeyModifier.Ctrl, Keys.A)
  17.  
  18.        ' Replaces the current registered hotkey with a new one. (Alt + Escape)
  19.        hotkey = New HotkeyManager(DirectCast([Enum].Parse(GetType(HotkeyManager.KeyModifier), "Alt", True), HotkeyManager.KeyModifier),
  20.                                   DirectCast([Enum].Parse(GetType(Keys), "Escape", True), Keys))
  21.  
  22.        ' Set the tag property.
  23.        hotkey.Tag = "I'm a String tag"
  24.  
  25.    End Sub
  26.  
  27.    ''' <summary>
  28.    ''' Handles the Press event of the HotKey object.
  29.    ''' </summary>
  30.    Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _
  31.    Handles hotkey.Press
  32.  
  33.        Dim sb As New System.Text.StringBuilder
  34.        With sb
  35.            .AppendLine(String.Format("Key.......: {0}", e.Key.ToString))
  36.            .AppendLine(String.Format("Modifier..: {0}", e.Modifier.ToString))
  37.            .AppendLine(String.Format("Identifier: {0}", e.Id))
  38.            .AppendLine(String.Format("Press-count: {0}", e.Count))
  39.            .AppendLine(String.Format("Tag: {0}", DirectCast(sender, HotkeyManager).Tag.ToString))
  40.        End With
  41.  
  42.        MessageBox.Show(sb.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
  43.  
  44.        ' Unregister the hotkey.
  45.        Me.hotkey.Unregister()
  46.  
  47.        ' Register it again.
  48.        Me.hotkey.Register()
  49.  
  50.        ' Is Registered?
  51.        Debug.WriteLine(Me.hotkey.IsRegistered)
  52.  
  53.    End Sub
  54.  
  55. End Class


Y aquí el código fuente:

Código:
' ***********************************************************************
' Author   : Elektro
' Modified : 17-March-2015
' ***********************************************************************
' <copyright file="HotkeyManager.vb" company="Elektro Studios">
'     Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************

#Region " Option Statements "

Option Explicit On
Option Strict On
Option Infer Off

#End Region

#Region " Imports "

Imports System.ComponentModel
Imports System.Runtime.InteropServices

#End Region

#Region " Global Hotkey "

''' <summary>
''' Manages a system-wide hotkey.
''' </summary>
Public NotInheritable Class HotkeyManager : Inherits NativeWindow : Implements IDisposable

#Region " P/Invoke "

    ''' <summary>
    ''' Platform Invocation methods (P/Invoke), access unmanaged code.
    ''' This class does not suppress stack walks for unmanaged code permission.
    ''' <see cref="System.Security.SuppressUnmanagedCodeSecurityAttribute"/>  must not be applied to this class.
    ''' This class is for methods that can be used anywhere because a stack walk will be performed.
    ''' MSDN Documentation: http://msdn.microsoft.com/en-us/library/ms182161.aspx
    ''' </summary>
    Private NotInheritable Class NativeMethods

        ''' <summary>
        ''' Defines a system-wide hotkey.
        ''' </summary>
        ''' <param name="hWnd">The hWND.</param>
        ''' <param name="id">The identifier of the hotkey.
        ''' If the hWnd parameter is NULL, then the hotkey is associated with the current thread rather than with a particular window.
        ''' If a hotkey already exists with the same hWnd and id parameters.</param>
        ''' <param name="fsModifiers">The keys that must be pressed in combination with the key specified by the uVirtKey parameter
        ''' in order to generate the WM_HOTKEY message.
        ''' The fsModifiers parameter can be a combination of the following values.</param>
        ''' <param name="vk">The virtual-key code of the hotkey.</param>
        ''' <returns>
        ''' <c>true</c> if the function succeeds, otherwise <c>false</c>
        ''' </returns>
        <DllImport("user32.dll", SetLastError:=True)>
        Public Shared Function RegisterHotKey(
                      ByVal hWnd As IntPtr,
                      ByVal id As Integer,
                      ByVal fsModifiers As UInteger,
                      ByVal vk As UInteger
        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        ''' <summary>
        ''' Unregisters a hotkey previously registered.
        ''' </summary>
        ''' <param name="hWnd">The hWND.</param>
        ''' <param name="id">The identifier of the hotkey to be unregistered.</param>
        ''' <returns>
        ''' <c>true</c> if the function succeeds, otherwise <c>false</c>
        ''' </returns>
        <DllImport("user32.dll", SetLastError:=True)>
        Public Shared Function UnregisterHotKey(
                      ByVal hWnd As IntPtr,
                      ByVal id As Integer
        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

    End Class

#End Region

#Region " Properties "

    ''' <summary>
    ''' Gets the key assigned to the hotkey.
    ''' </summary>
    ''' <value>The key assigned to the hotkey.</value>
    Public ReadOnly Property Key As Keys
        Get
            Return Me.pressEventArgs.Key
        End Get
    End Property

    ''' <summary>
    ''' Gets the key-modifier assigned to the hotkey.
    ''' </summary>
    ''' <value>The key-modifier assigned to the hotkey.</value>
    Public ReadOnly Property Modifier As KeyModifier
        Get
            Return Me.pressEventArgs.Modifier
        End Get
    End Property

    ''' <summary>
    ''' Gets the unique identifier assigned to the hotkey.
    ''' </summary>
    ''' <value>The unique identifier assigned to the hotkey.</value>
    Public ReadOnly Property ID As Integer
        Get
            Return Me.pressEventArgs.Id
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets the user-defined data associated with this object.
    ''' </summary>
    ''' <value>The user-defined data associated with this object.</value>
    Public Property Tag As Object

    ''' <summary>
    ''' Gets a value that specifies how many times was pressed the hotkey.
    ''' </summary>
    ''' <value>A value that specifies how many times was pressed the hotkey.</value>
    Public ReadOnly Property Count As Integer
        Get
            Return Me.count1
        End Get
    End Property
    ''' <summary>
    ''' A value that specifies how many times was pressed the hotkey.
    ''' </summary>
    Private count1 As Integer

#End Region

#Region " Enumerations "

    ''' <summary>
    ''' Specifies the avaliable key-modifiers to assign for a hotkey.
    ''' </summary>
    <FlagsAttribute>
    Public Enum KeyModifier As Integer

        ''' <summary>
        ''' Any modifier.
        ''' </summary>
        None = &H0

        ''' <summary>
        ''' The Alt key.
        ''' </summary>
        Alt = &H1

        ''' <summary>
        ''' The Control key.
        ''' </summary>
        Ctrl = &H2

        ''' <summary>
        ''' The Shift key.
        ''' </summary>
        Shift = &H4

        ''' <summary>
        ''' The Windows key.
        ''' </summary>
        Win = &H8

    End Enum

    ''' <summary>
    ''' Specifies A Windows Message Identifier.
    ''' </summary>
    Private Enum WindowsMessages As Integer

        ''' <summary>
        ''' Posted when the user presses a hot key registered by the RegisterHotKey function.
        ''' The message is placed at the top of the message queue associated with the thread that registered the hot key.
        ''' <paramref name="WParam"/>
        ''' The identifier of the hot key that generated the message.
        ''' If the message was generated by a system-defined hot key.
        ''' <paramref name="LParam"/>
        ''' The low-order word specifies the keys that were to be pressed in
        ''' combination with the key specified by the high-order word to generate the WM_HOTKEY message.
        ''' </summary>
        WM_HOTKEY = &H312

    End Enum

#End Region

#Region " Events "

    ''' <summary>
    ''' Event that is raised when a hotkey is pressed.
    ''' </summary>
    Public Event Press As EventHandler(Of HotKeyEventArgs)

    ''' <summary>
    ''' Stores the Press Event Arguments.
    ''' </summary>
    Private ReadOnly pressEventArgs As HotKeyEventArgs

    ''' <summary>
    ''' Event arguments for the Press event.
    ''' </summary>
    Public NotInheritable Class HotKeyEventArgs : Inherits EventArgs

        ''' <summary>
        ''' Gets or sets the key assigned to the hotkey.
        ''' </summary>
        ''' <value>The key assigned to the hotkey.</value>
        Public Property Key As Keys

        ''' <summary>
        ''' Gets or sets the key-modifier assigned to the hotkey.
        ''' </summary>
        ''' <value>The key-modifier assigned to the hotkey.</value>
        Public Property Modifier As KeyModifier

        ''' <summary>
        ''' Gets or sets the unique identifier assigned to the hotkey.
        ''' </summary>
        ''' <value>The unique identifier assigned to the hotkey.</value>
        Public Property Id As Integer

        ''' <summary>
        ''' Gets or sets how many times was pressed the hotkey.
        ''' </summary>
        Public Property Count As Integer

    End Class

#End Region

#Region " Exceptions "

    ''' <summary>
    ''' Exception that is thrown when a hotkey tries to register but is already registered.
    ''' </summary>
    <Serializable>
    Private NotInheritable Class IsRegisteredException : Inherits Exception

        ''' <summary>
        ''' Initializes a new instance of the <see cref="IsRegisteredException"/> class.
        ''' </summary>
        Sub New()
            MyBase.New("Unable to register because the hotkey is already registered.")
        End Sub

    End Class

    ''' <summary>
    ''' Exception that is thrown when a hotkey tries to unregister but is not registered.
    ''' </summary>
    <Serializable>
    Private NotInheritable Class IsNotRegisteredException : Inherits Exception

        ''' <summary>
        ''' Initializes a new instance of the <see cref="IsNotRegisteredException"/> class.
        ''' </summary>
        Sub New()
            MyBase.New("Unable to unregister because the hotkey is not registered.")
        End Sub

    End Class

#End Region

#Region " Constructors "

    ''' <summary>
    ''' Creates a new system-wide hotkey.
    ''' </summary>
    ''' <param name="Modifier">
    ''' Indicates the key-modifier to assign to the hotkey.
    ''' ( Can use one or more modifiers )
    ''' </param>
    ''' <param name="Key">
    ''' Indicates the key to assign to the hotkey.
    ''' </param>
    ''' <exception cref="IsRegisteredException"></exception>
    <DebuggerStepperBoundary()>
    Public Sub New(ByVal modifier As KeyModifier,
                   ByVal key As Keys)

        MyBase.CreateHandle(New CreateParams)

        Me.pressEventArgs = New HotKeyEventArgs
        With Me.pressEventArgs
            .Id = MyBase.GetHashCode()
            .Key = key
            .Modifier = modifier
            .Count = 0
        End With

        If Not NativeMethods.RegisterHotKey(MyBase.Handle,
                                            Me.ID,
                                            CUInt(Me.Modifier),
                                            CUInt(Me.Key)) Then

            Throw New IsRegisteredException

        End If

    End Sub

#End Region

#Region " Event-Handlers "

    ''' <summary>
    ''' Occurs when a hotkey is pressed.
    ''' </summary>
    Private Sub OnHotkeyPress() Handles Me.Press
        Me.count1 += 1
    End Sub

#End Region

#Region " Public Methods "

    ''' <summary>
    ''' Determines whether this hotkey is registered on the system.
    ''' </summary>
    ''' <returns>
    ''' <c>true</c> if this hotkey is registered; otherwise, <c>false</c>.
    ''' </returns>
    Public Function IsRegistered() As Boolean

        Me.DisposedCheck()

        ' Try to unregister the hotkey.
        Select Case NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID)

            Case False ' Unregistration failed.
                Return False ' Hotkey is not registered.

            Case Else ' Unregistration succeeds.
                Me.Register() ' Re-Register the hotkey before return.
                Return True ' Hotkey is registeres.

        End Select

    End Function

    ''' <summary>
    ''' Registers this hotkey on the system.
    ''' </summary>
    ''' <exception cref="IsRegisteredException"></exception>
    Public Sub Register()

        Me.DisposedCheck()

        If Not NativeMethods.RegisterHotKey(MyBase.Handle,
                                            Me.ID,
                                            CUInt(Me.Modifier),
                                            CUInt(Me.Key)) Then

            Throw New IsRegisteredException

        End If

    End Sub

    ''' <summary>
    ''' Unregisters this hotkey from the system.
    ''' After calling this method the hotkey turns unavaliable.
    ''' </summary>
    ''' <param name="resetCounter">If set to <c>true</c>, resets the htkey press count.</param>
    ''' <exception cref="WindowsApplication2.HotkeyManager.IsNotRegisteredException"></exception>
    Public Sub Unregister(Optional ByVal resetCounter As Boolean = False)

        Me.DisposedCheck()

        If Not NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID) Then
            Throw New IsNotRegisteredException
        End If

        If resetCounter Then
            Me.pressEventArgs.Count = 0
        End If

    End Sub

#End Region

#Region " Hidden methods "

    ''' <summary>
    ''' Assigns a handle to this window.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Sub AssignHandle(handle As IntPtr)
        MyBase.AssignHandle(handle)
    End Sub

    ''' <summary>
    ''' Creates a window and its handle with the specified creation parameters.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Sub CreateHandle(cp As CreateParams)
        MyBase.CreateHandle(cp)
    End Sub

    ''' <summary>
    ''' Destroys the window and its handle.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Sub DestroyHandle()
        MyBase.DestroyHandle()
    End Sub

    ''' <summary>
    ''' Releases the handle associated with this window.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Sub ReleaseHandle()
        MyBase.ReleaseHandle()
    End Sub

    ''' <summary>
    ''' Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function CreateObjRef(requestedType As Type) As System.Runtime.Remoting.ObjRef
        Return MyBase.CreateObjRef(requestedType)
    End Function

    ''' <summary>
    ''' Invokes the default window procedure associated with this window.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Sub DefWndProc(ByRef m As Message)
        MyBase.DefWndProc(m)
    End Sub

    ''' <summary>
    ''' Retrieves the current lifetime service object that controls the lifetime policy for this instance.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function GetLifeTimeService() As Object
        Return MyBase.GetLifetimeService
    End Function

    ''' <summary>
    ''' Obtains a lifetime service object to control the lifetime policy for this instance.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function InitializeLifeTimeService() As Object
        Return MyBase.InitializeLifetimeService
    End Function

    ''' <summary>
    ''' Serves as a hash function for a particular type.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function GetHashCode() As Integer
        Return MyBase.GetHashCode
    End Function

    ''' <summary>
    ''' Gets the System.Type of the current instance.
    ''' </summary>
    ''' <returns>The exact runtime type of the current instance.</returns>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function [GetType]() As Type
        Return MyBase.GetType
    End Function

    ''' <summary>
    ''' Determines whether the specified System.Object instances are considered equal.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function Equals(ByVal obj As Object) As Boolean
        Return MyBase.Equals(obj)
    End Function

    ''' <summary>
    ''' Returns a String that represents the current object.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Function ToString() As String
        Return MyBase.ToString
    End Function

    ''' <summary>
    ''' Gets the handle for this window.
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Never)>
    Public Shadows Property Handle() As IntPtr

#End Region

#Region " WndProc "

    ''' <summary>
    ''' Invokes the default window procedure associated with this window to process messages.
    ''' </summary>
    ''' <param name="m">
    ''' A <see cref="T:System.Windows.Forms.Message"/> that is associated with the current windows message.
    ''' </param>
    Protected Overrides Sub WndProc(ByRef m As Message)

        Select Case m.Msg

            Case WindowsMessages.WM_HOTKEY  ' A hotkey is pressed.

                ' Update the pressed counter.
                Me.pressEventArgs.Count += 1

                ' Raise the Event
                RaiseEvent Press(sender:=Me, e:=Me.pressEventArgs)

            Case Else
                MyBase.WndProc(m)

        End Select

    End Sub

#End Region

#Region " IDisposable "

    ''' <summary>
    ''' To detect redundant calls when disposing.
    ''' </summary>
    Private isDisposed As Boolean = False

    ''' <summary>
    ''' Prevent calls to methods after disposing.
    ''' </summary>
    ''' <exception cref="System.ObjectDisposedException"></exception>
    Private Sub DisposedCheck()

        If Me.isDisposed Then
            Throw New ObjectDisposedException(Me.GetType().FullName)
        End If

    End Sub

    ''' <summary>
    ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    ''' </summary>
    Public Sub Dispose() Implements IDisposable.Dispose
        Me.Dispose(isDisposing:=True)
        GC.SuppressFinalize(Me)
    End Sub

    ''' <summary>
    ''' Releases unmanaged and - optionally - managed resources.
    ''' </summary>
    ''' <param name="IsDisposing">
    ''' <c>true</c> to release both managed and unmanaged resources;
    ''' <c>false</c> to release only unmanaged resources.
    ''' </param>
    Protected Sub Dispose(ByVal isDisposing As Boolean)

        If Not Me.isDisposed Then

            If isDisposing Then
                NativeMethods.UnregisterHotKey(MyBase.Handle, Me.ID)
            End If

        End If

        Me.isDisposed = True

    End Sub

#End Region

End Class

#End Region

Bien, la idea sería que compilases el código a una librería.dll (lo puedes hacer con Visual Studio, creando un proyecto de tipo 'Class Library' y copiando el source de mi código)

Una vez tengas compilado el ensamblado .Net (librería.dll), solo tienes que utilizar alguna herramienta cómo JNA, JNI, o JavoNet para poder utilizarla desde tu proyecto Java.

Lo cierto es que yo no se nada de Java, pero si te ha gustado la idea entonces te intentaré orientar cómo pueda...

Se dice que JNA es mejor y más sencillo de implementar que JNI, así que tomemos cómo referencia JNA,
lo que debes hacer (según explican aquí) es crear una interface que servirá cómo wrapper de la librería .net, tal que así (esto es solo un ejemplo):

Código
  1. public interface NativeExample{
  2.  
  3.  public int method1(String param1);
  4.  public boolean mehthod2();
  5.  
  6. }

Y así para importar la librería .net:
Código
  1. NativeExample nativeExample = (NativeExample) Native.loadLibrary("librería", NativeExample.class);

Bien, si te fijas, la funcionalidad principal de la class desarrollada en .Net la expongo a traves de un constructor (o ctor):
Citar
Código
  1. ' Registers a new global hotkey on the system. (Alt + Ctrl + A)
  2. hotkey = New HotkeyManager(HotkeyManager.KeyModifier.Alt Or HotkeyManager.KeyModifier.Ctrl, Keys.A)

En la interface de Java debes especificar la misma parametización que utiliza el ctor de mi Class.

El método New acepta dos parámetros de tipo Integer (o Int32), el primer parámetro es un valor de esta enumeración del código fuente, que especifica un modificador de tecla:
Citar
Código
  1. ''' <summary>
  2. ''' Specifies the avaliable key-modifiers to assign for a hotkey.
  3. ''' </summary>
  4. <FlagsAttribute>
  5. Public Enum KeyModifier As Integer
  6.  
  7.    ''' <summary>
  8.    ''' Any modifier.
  9.    ''' </summary>
  10.    None = &H0
  11.  
  12.    ''' <summary>
  13.    ''' The Alt key.
  14.    ''' </summary>
  15.    Alt = &H1
  16.  
  17.    ''' <summary>
  18.    ''' The Control key.
  19.    ''' </summary>
  20.    Ctrl = &H2
  21.  
  22.    ''' <summary>
  23.    ''' The Shift key.
  24.    ''' </summary>
  25.    Shift = &H4
  26.  
  27.    ''' <summary>
  28.    ''' The Windows key.
  29.    ''' </summary>
  30.    Win = &H8
  31.  
  32. End Enum

Nota: Puedes combinar los valores de la enumeración, por ejemplo 4+2 = Shift+Control

El segundo parámetro sería un valor de la enumeración System.Windows.Forms.Keys de la librería de clases de .Net Framework ( documentación: http://msdn.microsoft.com/de-de/library/system.windows.forms.keys%28v=vs.110%29.aspx ), con este pequeño código en VB.Net sacamos los valores Int32:

Código
  1. For Each value As Integer In [Enum].GetValues(GetType(Keys))
  2.    Debug.WriteLine(String.Format("key: {0}", [Enum].GetName(GetType(Keys), value)))
  3.    Debug.WriteLine(String.Format("val: {0}", value))
  4.    Debug.WriteLine(String.Empty)
  5. Next value

Output:
http://pastebin.com/UyTEVAQv

Con esa implementación ya habrías conseguido registrar el Hotkey desde esta libreria .Net, pero aun faltaría lo más importante, asociar el hotkey a una acción determinada.

En mi Class expongo el evento HotkeyManager.Press que se dispara al interceptar el mensaje de ventana WM_HOTKEY (documentación: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646279%28v=vs.85%29.aspx) y se puede handlear a traves de la instancia del objeto, como viste en este ejemplo:

Citar
Código
  1.    ''' <summary>
  2.    ''' Handles the Press event of the HotKeyManager instance.
  3.    ''' </summary>
  4.    Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _
  5.    Handles hotkey.Press
  6.  
  7.    End Sub

Las instrucciones del interior de ese bloque de código se procesarían cuando se presione el hotkey que haya sido registrado en el sistema,
y hasta aquí es donde puedo intentar orientarte, por que no tengo ni idea de cómo exponer eventos definidos en una clase de .Net a traves de Java (ni la firma del evento), deberías preguntar y/o documentarte por ti mismo.

Un saludo!
« Última modificación: 17 Marzo 2015, 06:49 am por Eleкtro » En línea

PabloPbl

Desconectado Desconectado

Mensajes: 188


Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #4 en: 17 Marzo 2015, 14:58 pm »

Muchísimas gracias Elektro. Por lo que veo tienes una librería hecha en VB.Net la cual la pasas a un dll y ese dll es como un puente para que desde Java pueda usar las funciones hechas en VB.Net solo que el Dll claro. Es bastante curioso.

Dicen que C/C++ son lenguajes que tienen bastante acceso al SO y pueden realizar un montón de cosas con el, hasta virus, pues tenia pensado en meterme mas a profundidad a lo que es C++, pues me gustaría hacer algo como lo que hiciste tú en VB.Net solo que desde C++, para usarlo desde java.

Si no es mucha molestia, me gustaría probar lo que dices, mas que nada por aprender eso del JNA, que me interesa bastante.

Salu2  ;D.
En línea

ThunderCls


Desconectado Desconectado

Mensajes: 455


Coder | Reverser | Gamer


Ver Perfil WWW
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #5 en: 17 Marzo 2015, 15:54 pm »

El q busca encuentra  :-\
https://truney.wordpress.com/2010/12/03/acceder-a-la-api-de-windows-desde-java/
En línea

-[ "…I can only show you the door. You're the one that has to walk through it." – Morpheus (The Matrix) ]-
http://reversec0de.wordpress.com
https://github.com/ThunderCls/
Usuario Invitado


Desconectado Desconectado

Mensajes: 625



Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #6 en: 17 Marzo 2015, 17:37 pm »

Eso no es práctico y no creo que sea funcional. Lo mejor que puedes hacer como te dijo Elektro es hacer implementar el KeyListener en otro lenguaje y mediante JNI o JNA. Te recomiendo que lo hagas en C/C++. Te dejo un enlace donde hay un ejemplo de esto.

Link: Java JNI C++ Hotkeys
En línea

"La vida es muy peligrosa. No por las personas que hacen el mal, si no por las que se sientan a ver lo que pasa." Albert Einstein
PabloPbl

Desconectado Desconectado

Mensajes: 188


Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #7 en: 17 Marzo 2015, 17:41 pm »

Muchas gracias [ Thunder | CLS ], estuve mirando la fuente que pusiste y la idea esta bastante buena la de no tener que recurrir a otros lenguajes.

Muchas gracias a todos, ya he conseguido la orientación que necesito para continuar solo, ahora solo me queda investigar sobre todo esto.

Jaja tienes razón el que busca encuentra, pero nunca se me hubiera ocurrido buscarlo con esos términos avanzados.

Un saludo a todos.
En línea

PabloPbl

Desconectado Desconectado

Mensajes: 188


Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #8 en: 17 Marzo 2015, 17:44 pm »

Muchas gracias Gus, es exactamente lo que quiero conseguir y con esa fuente aún mejor.

Saludos  ;D!!
En línea

1mpuls0


Desconectado Desconectado

Mensajes: 1.186


Ver Perfil
Re: [Duda]Es posible detectar teclas si mi aplicacion no tiene el foco
« Respuesta #9 en: 26 Marzo 2015, 23:14 pm »

JNI

Aquí hay algo que te puede servir.
https://foro.elhacker.net/java/tutorial_keylogger_en_java_jni-t275565.0.html
En línea

abc
Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines