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:
Public Class Form1
''' <summary>
''' Define the system-wide hotkey object.
''' </summary>
Private WithEvents hotkey As HotkeyManager
''' <summary>
''' Initializes a new instance of this class.
''' </summary>
Public Sub Test() Handles MyBase.Shown
MyClass.InitializeComponent()
' Registers a new global hotkey on the system. (Alt + Ctrl + A)
hotkey = New HotkeyManager(HotkeyManager.KeyModifier.Alt Or HotkeyManager.KeyModifier.Ctrl, Keys.A)
' Replaces the current registered hotkey with a new one. (Alt + Escape)
hotkey = New HotkeyManager(DirectCast([Enum].Parse(GetType(HotkeyManager.KeyModifier), "Alt", True), HotkeyManager.KeyModifier),
DirectCast([Enum].Parse(GetType(Keys), "Escape", True), Keys))
' Set the tag property.
hotkey.Tag = "I'm a String tag"
End Sub
''' <summary>
''' Handles the Press event of the HotKey object.
''' </summary>
Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _
Handles hotkey.Press
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine(String.Format("Key.......: {0}", e.Key.ToString))
.AppendLine(String.Format("Modifier..: {0}", e.Modifier.ToString))
.AppendLine(String.Format("Identifier: {0}", e.Id))
.AppendLine(String.Format("Press-count: {0}", e.Count))
.AppendLine(String.Format("Tag: {0}", DirectCast(sender, HotkeyManager).Tag.ToString))
End With
MessageBox.Show(sb.ToString, String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
' Unregister the hotkey.
Me.hotkey.Unregister()
' Register it again.
Me.hotkey.Register()
' Is Registered?
Debug.
WriteLine(Me.
hotkey.
IsRegistered)
End Sub
End Class
Y aquí el código fuente:
' ***********************************************************************
' 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):
public interface NativeExample{
public int method1
(String param1
); public boolean mehthod2();
}
Y así para importar la librería .net:
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):
' Registers a new global hotkey on the system. (Alt + Ctrl + A)
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:
''' <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
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:
For Each value As Integer In [Enum].GetValues(GetType(Keys))
Debug.
WriteLine(String.
Format("key: {0}",
[Enum].
GetName(GetType(Keys
), value
))) Debug.
WriteLine(String.
Format("val: {0}", value
)) Debug.
WriteLine(String.
Empty) Next value
Output:
http://pastebin.com/UyTEVAQvCon 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:
''' <summary>
''' Handles the Press event of the HotKeyManager instance.
''' </summary>
Private Sub HotKey_Press(ByVal sender As Object, ByVal e As HotkeyManager.HotKeyEventArgs) _
Handles hotkey.Press
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!