21

Javaでキーリスナーを書きたい。Javaアプリにフォーカスがあるかどうかに関係なく、すべてのキー押下を追跡する必要があります。これは可能ですか?

4

6 に答える 6

17

可能ですが、常に移植可能であるとは限らないJNIを利用した実装が必要です。

Java System Hookは、Windows32および64ビットで動作するそのようなライブラリの1つです。

于 2012-08-31T12:22:58.617 に答える
16

少し前にそういうものを使いました。Javaシステムフックを見てください。

JNIを介して(Javaアプリケーションだけでなく)グローバルキーの押下を追跡します。それは私にとって速くそして完璧に働きました


KeyListenerはデーモンスレッドとして起動しますが、これはアプリケーションを存続させません。プログラムにデーモン以外のスレッドをもう1つ実行する必要があります

例えば:

GlobalKeyListener globalKeyListener = new GlobalKeyListener();
globalKeyListener.addKeyListener(listener);
while (true) {
    Thread.sleep(1000);
}
于 2012-08-29T12:18:43.597 に答える
5

いいえ、Javaはアクティブウィンドウ外のキーストロークにアクセスできません。

これを実現するには、Javaとネイティブコードの組み合わせを使用し、JNIを使​​用してネイティブコードを呼び出す必要があります。Microsoft Windowsでは、を確認する必要がありますGetAsyncKeyState(key)

于 2012-08-29T12:09:42.773 に答える
3

非常に可能ですが、標準のJavaを使用しない場合は、ネイティブレベルでオペレーティングシステムを操作する必要があります。

これは、JNAを使用してWindowsUser32APIのSetWindowsHookEx関数にアクセスするWindowsのトピックです。

于 2012-08-29T12:25:06.517 に答える
2

いいえ、Javaはアクティブな(フォーカスされた)ウィンドウでない場合、キーストークをリッスンしないため、それを行うことはできません。

于 2012-08-29T12:03:55.793 に答える
-5
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * keylogger.java
 *
 * Created on Mar 8, 2012, 8:12:20 AM
 */

package main;

import javax.swing.JOptionPane;

/**
 *
 * @author Lab-Admin
 */

public class keylogger extends javax.swing.JFrame {

    /** Creates new form keylogger */
    public keylogger() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        userfield = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        passfield = new javax.swing.JPasswordField();
        btnlogin = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Punk Academy PRoject");
        setBackground(new java.awt.Color(255, 0, 0));
        setFont(new java.awt.Font("Algerian", 1, 14)); // NOI18N
        setForeground(new java.awt.Color(255, 153, 0));

        jPanel1.setBackground(new java.awt.Color(255, 204, 0));

        jLabel1.setBackground(new java.awt.Color(255, 0, 0));
        jLabel1.setFont(new java.awt.Font("Algerian", 1, 24)); // NOI18N
        jLabel1.setText("Punk Academy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(jLabel1)
                .addContainerGap(108, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\umbrellalast.gif")); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("username");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("password");

        btnlogin.setIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setText("Log in");
        btnlogin.setDisabledIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setDisabledSelectedIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setIconTextGap(2);
        btnlogin.setPressedIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setRolloverIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setRolloverSelectedIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.setSelectedIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\Keys.gif")); // NOI18N
        btnlogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnloginActionPerformed(evt);
            }
        });

        btncancel.setIcon(new javax.swing.ImageIcon("D:\\COPRO OLAER\\enrollment system\\exit.png")); // NOI18N
        btncancel.setText("Cancel");
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap(82, Short.MAX_VALUE)
                        .addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(passfield, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(userfield, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(btncancel, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(passfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))))
                .addGap(49, 49, 49)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnlogin)
                    .addComponent(btncancel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnloginActionPerformed

        if(evt.getSource()==btnlogin)
        {
            String user = btnlogin.getText();
            String pass = btncancel.getText();

            if(user.equals(user)&&pass.equals(pass))
            {
                this.hide();
                JOptionPane.showMessageDialog(null,"ACCESS GRANTED");
                welcome s2 = new welcome();
                s2.show();
            }
            else
            {
                JOptionPane.showMessageDialog(null,"ACCESS DENIED");
            }
        }

    }//GEN-LAST:event_btnloginActionPerformed

    private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelActionPerformed
        if(evt.getSource()==btncancel)
        {
            JOptionPane.showMessageDialog(null,"BYE! BYE!");

            System.exit(0);
        }
    }//GEN-LAST:event_btncancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new keylogger().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btncancel;
    private javax.swing.JButton btnlogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField passfield;
    private javax.swing.JTextField userfield;
    // End of variables declaration//GEN-END:variables
}
于 2014-08-15T22:33:20.200 に答える