I want to make a Keylogger happen in Java Swing. I could manage to make a simple keylogger that catches Keystrokes and writes them to a logfile, when the focus is on the Swing Window. Since the focus has to be on the window, is it possible to make an invisible JFrame, that is always on top?
Something like this:
public class Logger extends JFrame implements KeyListener
logger.setVisible(false);
logger.setAlwaysOnTop(true);
Is this possible in Java?
So, you want a key logger that record keystroke even the focus is not on Java Swing window, right?
Java itself cannot do this, but Java Native Interface (JNI) can. You could Google "Java System Hook" and download the JAR from Github. (https://github.com/kristian/system-hook/releases) In one word, it keeps recording your keyboard as long as the program runs, regardless of which window you are focusing on.
The way I use it: I wrote a program that keep running while I play games and it can print out whatever key I press in the game.
Related
I made a simple JFrame with Swing. I want to know how I would go about making a non-visible input that would open up another JFrame I have in another class. (Like cheat codes in video games, you enter a combination and something happens.) I am not sure how to capture the user input without a text field.
You should use keybinding attached to your JFrame. You would want to store keystrokes as a String internally and after each keystroke, see whether the user has entered a recognizable cheat code or just listen for a return keypress as a delimiter for the code.
Keep in mind that if a component within that JFrame has focus and also implements the same key bindings, then that component will take precedence over the JFrame, effectively intercepting the keystrokes.
How can I keep my JFrame listening for KeyEvent while its state is iconified?
For example after pressing F1 to start the program (an auto clicker) and iconify the frame (remember that because of the click the focus on the frame is lost!) :)
If you're trying to create a key-logger program with Java, don't. It's not a good tool for creating this since it by design is isolated from the OS, in other words it's "OS-agnostic". You will need to use a different tool for your allegedly nefarious plans such as C or C++ since these languages allow you to get closer to the "metal".
I created a text-based game similar to Zork and I need a gui to run it outside of Eclipse. I want to run it as a jar. (by the way I'm on a mac if that changes anything). I only need an output field and an input field. What would be the easiest way to achieve this?
And how much of my code would I need to change? (I used System.out.print for output and a Scanner for input)
If you want to crate GUI like console the simple way to do it is to add textarea component to your frame or or panel that has scroll bars through the viewport. Create a stream that feeds the component with text. Then simply redirect standard output to that stream. Finish. Start your GUI and enjoy the console.
If you don't want to run this on a terminal, you should probably use Swing with a JTextArea in which you append all the messages to the user, and a simple JTextField for the user to enter his commands.
Here's a quick example of JTextArea so you get an idea. You'll need to read more about events on Swing to make things like reacting to the user pressing the ENTER key to read the contents of the text field and run the game logic.
Note that the screenshot on the example above uses the "Metal" look and feel, but it should look much closer to a native application on the Mac.
I have a weird issue... I'm a relatively new "enthusiast" Java programmer (I used to make my living hacking Perl, in a previous career), working on my first semi-real application. "Main-Class" is the MyApp class, which creates a UserInputDialog instance.
UserInputDialog is a class I wrote that extends JFrame, implements ActionListener and KeyListener, uses FlowLayout, and presents the user with a JLabel, JTextField, and Cancel/OK JButtons. When the JTextField generates a KeyEvent where keyReleased() == KeyEvent.VK_ENTER, or when the "OK" JButton generates an ActionEvent, UserInputDialog does some input validation, calls setVisible(false), and then calls MyApp.doSomething( JTextFieldInstance.getText() ).
That all works perfectly. But now I'm trying to add a progress window to MyApp, as doSomething() can occasionally take a fair amount of time to complete.
I created the ProgressWindow class, which extends JFrame, uses BorderLayout, and tosses a JProgressBar in .NORTH and a JScrollPane (wrapping a JTextArea) in .CENTER. ProgressWindow works perfectly when instantiated from ProgressWindowTester and fed test data. It also works fine if I copy-and-paste the test for loops from ProgressWindowTester into MyApp and don't have MyApp instantiate UserInputDialog (i.e., there's nothing inherent in MyApp that's causing this behavior; it seems to be some sort of interaction I'm not understanding, between UserInputDialog and ProgressWindow).
But when I try to use ProgressWindow in MyApp as intended, i.e., ProgressWindow setVisible(true), I get a blank Swing window (of the proper size, and with the title bar set properly). The JProgressBar and JScrollPane / JTextArea components don't appear. The ProgressWindow methods are being called by MyApp properly (System.err.println() messages show proper interaction), everything appears to be working fine, just, the components that should be visible in ProgressWindow ... aren't.
I can post code snippets, but it's kind of convoluted, and I'm probably just missing something obvious...
I'm familiar with the concept of separating UI and business logic generally (e.g., I used HTML::Template and Class::DBI and CGI::Application when building Perl applications), but I'm not sure I'm "doing it right" in Java...
Thanks in advance!
Oh, I get exactly the same behavior on the two environments I've tried the code in: javac 1.6.0_29 on Mac OS X 10.6.8 ("Snow Leopard"); and javac 1.7.0_02[1] on the Fedora 15 Linux distribution, kernel 2.6.31.10-3, LXDE desktop environment.
[1] Downloaded directly from oracle.com; I;m not using OpenJDK (I know JDK 7 is based on OpenJDK) or gcj or anything like that
You've got a concurrency in Swing issue where you're trying to do a long running process on the Swing event thread or EDT. Since this thread is responsible for drawing all components and for processing user input, if it is tied down by your long-running process, your GUI will be effectively frozen until the process is complete. The key is to use a background thread such as that provided by a SwingWorker for long-running processes, so the event thread doesn't get locked. Check out Concurrency in Swing for more information on this. Also check out the JProgressBar Tutorial for other insights on how to use progress bars with background threads.
Also, you'll not want to use a JFrame where a dialog, such as a JDialog, is much more appropriate. Also, you'll want to avoid use of KeyListeners with Swing. Much better would be to simply add an ActionListener to your JTextField since its default behavior is to respond to presses of the key.
Oh, and welcome to StackOverflow.com!
I was curious if it was possible to create hotkeys in a jApplet that are global for the applet...like a global key handler or something.
For example, I would like to be able to press the key, 'F11' lets say, then have a jPanel appear, and then press 'f11' again to make it disappear... regardless of which component I'm focused on.I don't expect it to work when I'm not focused on the application, just within.
thanks.
You need to use Key Bindings.