Skip to content

Commit

Permalink
New key listeners library
Browse files Browse the repository at this point in the history
  • Loading branch information
Exslims committed Aug 12, 2017
1 parent 4b176d0 commit 7d9550d
Show file tree
Hide file tree
Showing 24 changed files with 851 additions and 673 deletions.
5 changes: 5 additions & 0 deletions app-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<artifactId>app-shared</artifactId>
<version>1.0.0.5</version>
</dependency>
<dependency>
<groupId>com.1stleg</groupId>
<artifactId>jnativehook</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public HotKeysConfigurationService(ProfileDescriptor selectedProfile) {
@Override
public Map<String, HotKeyDescriptor> getDefault() {
Map<String, HotKeyDescriptor> keyMap = new HashMap<>();
keyMap.put(HotKeyType.INVITE_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',true,false,false,false));
keyMap.put(HotKeyType.TRADE_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_2,'2',true,false,false,false));
keyMap.put(HotKeyType.KICK_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_3,'3',true,false,false,false));
keyMap.put(HotKeyType.STILL_INTERESTING.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_4,'4',true,false,false,false));
keyMap.put(HotKeyType.CLOSE_NOTIFICATION.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_5,'5',true,false,false,false));
keyMap.put(HotKeyType.EXPAND_ALL.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',false,false,true,false));
keyMap.put("button_1",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',false,true,false,false));
keyMap.put("button_2",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'2',false,true,false,false));
keyMap.put("button_3",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'3',false,true,false,false));
keyMap.put("button_4",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'4',false,true,false,false));
keyMap.put(HotKeyType.INVITE_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',true,false,false));
keyMap.put(HotKeyType.TRADE_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_2,'2',true,false,false));
keyMap.put(HotKeyType.KICK_PLAYER.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_3,'3',true,false,false));
keyMap.put(HotKeyType.STILL_INTERESTING.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_4,'4',true,false,false));
keyMap.put(HotKeyType.CLOSE_NOTIFICATION.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_5,'5',true,false,false));
keyMap.put(HotKeyType.EXPAND_ALL.name(),new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',false,false,true));
keyMap.put("button_1",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'1',false,true,false));
keyMap.put("button_2",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'2',false,true,false));
keyMap.put("button_3",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'3',false,true,false));
keyMap.put("button_4",new HotKeyDescriptor("",GlobalKeyEvent.VK_1,'4',false,true,false));
return keyMap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
@AllArgsConstructor
@NoArgsConstructor
public class HotKeyDescriptor implements Serializable{
private String title = "";
private String title = "...";
private int virtualKeyCode;
private char keyChar;
private boolean menuPressed;
private boolean shiftPressed;
private boolean controlPressed;
private boolean extendedKey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@EqualsAndHashCode(callSuper = true)
@Data
public class AdrCaptureDescriptor extends AdrColoredComponentDescriptor implements Serializable {
private int fps = 1;
private int fps = 5;
private Dimension captureSize = new Dimension(64,64);
private Point captureLocation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ private HotKeyDescriptor convert(GlobalKeyEvent event) {
descriptor.setKeyChar(event.getKeyChar());
descriptor.setVirtualKeyCode(event.getVirtualKeyCode());
descriptor.setControlPressed(event.isControlPressed());
descriptor.setExtendedKey(event.isExtendedKey());
descriptor.setMenuPressed(event.isMenuPressed());
descriptor.setShiftPressed(event.isShiftPressed());
return descriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
package com.mercury.platform.shared.hotkey;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

import com.mercury.platform.shared.config.descriptor.HotKeyDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import lc.kra.system.keyboard.GlobalKeyboardHook;
import lc.kra.system.mouse.GlobalMouseHook;
import lc.kra.system.mouse.event.GlobalMouseAdapter;
import lc.kra.system.mouse.event.GlobalMouseEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HotKeysInterceptor {
public HotKeysInterceptor() {
GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(false);
keyboardHook.addKeyListener(new HotKeyAdapter());
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);

GlobalMouseHook mouseHook = new GlobalMouseHook(false);
mouseHook.addMouseListener(new GlobalMouseAdapter() {
@Override
public void mousePressed(GlobalMouseEvent globalMouseEvent) {
MercuryStoreCore.hotKeySubject.onNext(this.convert(globalMouseEvent));
}
private HotKeyDescriptor convert(GlobalMouseEvent event) {
HotKeyDescriptor descriptor = new HotKeyDescriptor();
switch (event.getButton()){
case 1: {
descriptor.setVirtualKeyCode(1000);
descriptor.setTitle("Mouse left");
break;
}
case 2: {
descriptor.setVirtualKeyCode(1002);
descriptor.setTitle("Mouse right");
break;
}
case 16: {
descriptor.setVirtualKeyCode(1016);
descriptor.setTitle("Mouse middle");
break;
}
}
return descriptor;
}
});
logger.setUseParentHandlers(false);
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
e.printStackTrace();
}
GlobalScreen.addNativeKeyListener(new MercuryNativeKeyListener());
GlobalScreen.addNativeMouseListener(new MercuryNativeMouseListener());
}

public static void main(String[] args) {
new HotKeysInterceptor();
// new HotKeysInterceptor();
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);

logger.setUseParentHandlers(false);
try {
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
e.printStackTrace();
}
GlobalScreen.addNativeMouseListener(new MercuryNativeMouseListener());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.mercury.platform.shared.hotkey;


import com.mercury.platform.shared.config.descriptor.HotKeyDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class MercuryNativeKeyListener implements NativeKeyListener{
private boolean menuPressed;
private boolean shiftPressed;
private boolean ctrlpressed;
@Override
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
switch (nativeKeyEvent.getKeyCode()){
case 42 : {
this.shiftPressed = true;
break;
}
case 29: {
this.ctrlpressed = true;
break;
}
case 56: {
this.menuPressed = true;
break;
}
default:{
HotKeyDescriptor hotKeyDescriptor = new HotKeyDescriptor();
hotKeyDescriptor.setTitle(NativeKeyEvent.getKeyText(nativeKeyEvent.getKeyCode()));
hotKeyDescriptor.setVirtualKeyCode(nativeKeyEvent.getKeyCode());
hotKeyDescriptor.setControlPressed(this.ctrlpressed);
hotKeyDescriptor.setShiftPressed(this.shiftPressed);
hotKeyDescriptor.setMenuPressed(this.menuPressed);

hotKeyDescriptor.setTitle(this.getButtonText(hotKeyDescriptor));
MercuryStoreCore.hotKeySubject.onNext(hotKeyDescriptor);
}
}
}

@Override
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
switch (nativeKeyEvent.getKeyCode()){
case 42 : {
this.shiftPressed = false;
break;
}
case 29: {
this.ctrlpressed = false;
break;
}
case 56: {
this.menuPressed = false;
break;
}
}
}

@Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {

}
private String getButtonText(HotKeyDescriptor descriptor){
String text = descriptor.getTitle();
if(descriptor.isShiftPressed())
text = "Shift + " + text;
if(descriptor.isMenuPressed())
text = "Alt + " + text;
if(descriptor.isControlPressed())
text = "Ctrl + " + text;
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mercury.platform.shared.hotkey;

import com.mercury.platform.shared.config.descriptor.HotKeyDescriptor;
import com.mercury.platform.shared.store.MercuryStoreCore;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseListener;

public class MercuryNativeMouseListener implements NativeMouseListener {
@Override
public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {
}

@Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
HotKeyDescriptor hotKeyDescriptor = new HotKeyDescriptor();
hotKeyDescriptor.setVirtualKeyCode(nativeMouseEvent.getButton() + 10000);
hotKeyDescriptor.setTitle(this.getModifiersText(nativeMouseEvent.getButton()));
MercuryStoreCore.hotKeySubject.onNext(hotKeyDescriptor);
}

@Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
}

private String getModifiersText(int code) {
switch (code) {
case 1: {
return "Mouse left";
}
case 2: {
return "Mouse right";
}
case 3: {
return "Mouse middle";
}
case 4: {
return "Button 4";
}
case 5: {
return "Button 5";
}
case 6: {
return "Button 6";
}
case 7: {
return "Button 7";
}
default: {
return "Undefined";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mercury.platform.shared;

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;

public class MainTestKeyHook {


public static void main(String[] args) throws Exception {
HOOKPROC hookProc = new HOOKPROC_bg();
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);

User32.HHOOK hHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, hookProc, hInst, 0);
if (hHook == null)
return;
User32.MSG msg = new User32.MSG();
System.err.println("Please press any key ....");
while (true) {
User32.INSTANCE.GetMessage(msg, null, 0, 0);
}
}
}

class HOOKPROC_bg implements HOOKPROC {

public HOOKPROC_bg() {
}

public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
System.err.println("callback bbbnhkilhjkibh nCode: " + nCode);
return new LRESULT(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public JPanel getCaptureLocationPanel(AdrCaptureDescriptor descriptor){
}

public JButton getHotKeyButton(AdrDurationComponentDescriptor descriptor) {
JButton button = this.componentsFactory.getBorderedButton(this.getButtonText(descriptor.getHotKeyDescriptor()));
JButton button = this.componentsFactory.getBorderedButton(descriptor.getHotKeyDescriptor().getTitle());
button.setFont(this.componentsFactory.getFont(FontStyle.BOLD, 18f));
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
Expand All @@ -192,7 +192,7 @@ public void mousePressed(MouseEvent e) {
} else {
descriptor.setHotKeyDescriptor(hotKey);
}
button.setText(getButtonText(descriptor.getHotKeyDescriptor()));
button.setText(hotKey.getTitle());
allowed = false;
MercuryStoreUI.adrReloadSubject.onNext(descriptor);
button.addMouseListener(mouseAdapter);
Expand Down Expand Up @@ -881,22 +881,6 @@ private JColorChooser getColorChooser(){
}
return colorChooser;
}
private String getButtonText(HotKeyDescriptor descriptor){
if(!descriptor.getTitle().equals("")){
return descriptor.getTitle();
}
if(descriptor.getKeyChar() == '\u0000') {
return "...";
}
String text = String.valueOf(descriptor.getKeyChar());
if(descriptor.isShiftPressed())
text = "Shift + " + text;
if(descriptor.isMenuPressed())
text = "Alt + " + text;
if(descriptor.isControlPressed())
text = "Ctrl + " + text;
return text;
}

private class ColorChooserMouseListener extends MouseAdapter {
private JPanel panel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mercury.platform.ui.components.panel.message;

/**
* Created by Константин on 10.08.2017.
*/
public class HistoryMessagePanel {
}
Loading

0 comments on commit 7d9550d

Please sign in to comment.