package imageTracking;



import com.sun.jna.Pointer;

import com.sun.jna.platform.win32.Kernel32;

import com.sun.jna.platform.win32.User32;

import com.sun.jna.platform.win32.WinDef.HMODULE;

import com.sun.jna.platform.win32.WinDef.LRESULT;

import com.sun.jna.platform.win32.WinDef.WPARAM;

import com.sun.jna.platform.win32.WinDef.LPARAM;

import com.sun.jna.platform.win32.WinUser;

import com.sun.jna.platform.win32.WinUser.HHOOK;

import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT;

import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;

import com.sun.jna.platform.win32.WinUser.MSG;


/** Sample implementation of a low-level keyboard hook on W32. */

class fss  {

   private static volatile boolean quit;

   private static HHOOK hhk;

   private static LowLevelKeyboardProc keyboardHook;


   public static void main(String[] args) {

       final User32 lib = User32.INSTANCE;

       HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);

       keyboardHook = new LowLevelKeyboardProc() {

           @Override

           public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

               if (nCode >= 0) {

                   switch(wParam.intValue()) {

                   case WinUser.WM_KEYUP:

                   case WinUser.WM_KEYDOWN:

                   case WinUser.WM_SYSKEYUP:

                   case WinUser.WM_SYSKEYDOWN:

                       System.err.println("in callback, key=" + info.vkCode);

                       if (info.vkCode == 81) {

                           quit = true;

                       }

                   }

               }


               Pointer ptr = info.getPointer();

               long peer = Pointer.nativeValue(ptr);

               return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));

           }

       };

       hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);

       System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");

       new Thread() {

           @Override

           public void run() {

               while (!quit) {

                   try { Thread.sleep(10); } catch(Exception e) { }

               }

               System.err.println("unhook and exit");

               lib.UnhookWindowsHookEx(hhk);

               System.exit(0);

           }

       }.start();


       // This bit never returns from GetMessage

       int result;

       MSG msg = new MSG();

       while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {

           if (result == -1) {

               System.err.println("error in get message");

               break;

           }

           else {

               System.err.println("got message");

               lib.TranslateMessage(msg);

               lib.DispatchMessage(msg);

           }

       }

       lib.UnhookWindowsHookEx(hhk);

   }

}


'JAVA > JNA' 카테고리의 다른 글

JNA사용을 위해 다운받아야될것들  (0) 2016.03.14
JNA 키보드이벤트 발생 keybd_event  (0) 2016.03.14
블로그 이미지

왕왕왕왕

,

JNA.JAR


Platform.jar 


2개만잇으면됨

두버전이 일치해야됨

'JAVA > JNA' 카테고리의 다른 글

JNA 로우레벨후킹 예제  (0) 2016.03.17
JNA 키보드이벤트 발생 keybd_event  (0) 2016.03.14
블로그 이미지

왕왕왕왕

,

package openCV;


import java.awt.Toolkit;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.ClipboardOwner;

import java.awt.datatransfer.DataFlavor;

import java.awt.datatransfer.StringSelection;

import java.awt.datatransfer.Transferable;


import com.sun.jna.Native;

import com.sun.jna.platform.win32.User32;

import com.sun.jna.platform.win32.WinDef.HWND;

import com.sun.jna.win32.StdCallLibrary;


public class te implements ClipboardOwner {

    public interface CustomUser32 extends StdCallLibrary {

        CustomUser32 INSTANCE = (CustomUser32) Native.loadLibrary("user32", CustomUser32.class);

        HWND GetForegroundWindow();

        void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    }


    public void lostOwnership(Clipboard clipboard, Transferable contents) {

        // dummy: needed for `ClipboardOwner`

    }


    void controlC(CustomUser32 customUser32) {

        customUser32.keybd_event((byte) 07/* VK_CONTROL*/, (byte) 0, 0, 0);

        customUser32.keybd_event((byte) 0x43 /* 'C' */, (byte) 0, 0, 0);

        customUser32.keybd_event((byte) 0x43 /* 'C' */, (byte) 0, 2 /* KEYEVENTF_KEYUP */, 0);

        customUser32.keybd_event((byte) 0x11 /* VK_CONTROL*/, (byte) 0, 2 /* KEYEVENTF_KEYUP */, 0);// 'Left Control Up

    }


    String getClipboardText() throws Exception {

        return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);

    }


    void setClipboardText(String data) throws Exception {

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(data), this);

    }


    String getSelectedText(User32 user32, CustomUser32 customUser32) throws Exception {

        HWND hwnd = customUser32.GetForegroundWindow();

        char[] windowText = new char[512];

        user32.GetWindowText(hwnd, windowText, 512);

        String windowTitle = Native.toString(windowText);

        System.out.println("Will take selected text from the following window: [" + windowTitle + "]");

        String before = getClipboardText();

        controlC(customUser32); // emulate Ctrl C

        Thread.sleep(100); // give it some time

        String text = getClipboardText();

        System.out.println("Currently in clipboard: " + text);

        // restore what was previously in the clipboard

        setClipboardText(before);

        return text;

    }


    public static void main(String[] args) throws Exception {

        te foo = new te();

        Thread.sleep(2000); // take some time for you to select something anywhere

        System.out.println(foo.getSelectedText(User32.INSTANCE, CustomUser32.INSTANCE));

    }

}

'JAVA > JNA' 카테고리의 다른 글

JNA 로우레벨후킹 예제  (0) 2016.03.17
JNA사용을 위해 다운받아야될것들  (0) 2016.03.14
블로그 이미지

왕왕왕왕

,