So this is probably an obvious question, but i am just wondering if there is a way to save a key press to a variable (like a char or a string). I am relatively new to java, I know how to use scanners and how to make hash codes, I got a login screen with no back button coded so yea making good progress, but with all that aside how do i save a key press to a variable?
Asked
Active
Viewed 35 times
0
-
Are you using a GUI or is this a Console (terminal) Application? – DevilsHnd - 退職した Apr 18 '23 at 00:50
-
Like you set any value to any variable ... sorry for the vagueness of the comment, but it matches the vagueness of the question. Why are you using `jnativehook`? That seems like a very specific requirement and to be honest, quite a technical one – MadProgrammer Apr 18 '23 at 01:09
-
The [documentation](https://github.com/kwhat/jnativehook/blob/2.2/doc/Keyboard.md) seems like a reasonable place to start, it shows you how to get the information from the API, from there it's just "standard" java variable assignment ... which raises more questions. If you're not sure how that works, maybe something like JNI/native hooks is best put aside till you do – MadProgrammer Apr 18 '23 at 01:11
-
@DevilsHnd for now this is a terminal application – megadriive Apr 20 '23 at 04:59
-
@MadProgrammer ya sorry if this is a lil vague but you can open a nativelistener and save the typed character to a variable? also i'm using jnativehook cause idk any other things that allow you to listen to keypresses lmao, please put me on if you know any other keypress detectors i could use. ^w^ ^w^ ^w^ – megadriive Apr 20 '23 at 05:02
-
[Language Basics: Variables](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html); [Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – MadProgrammer Apr 20 '23 at 05:20
-
@MadProgrammer i feel confident i know how variables work, and i stay spamming the static keyword to use variables in inputmismatchexception circumventors. looking back to this question i think ima try to assign the variable in one of the methods and see if that works, maybe my mistake was trying to assign the variable in the main method lmaooo. – megadriive Apr 20 '23 at 22:01
-
@MadProgrammer also wanna add, now reading the class members doc to understand how the private keyword even works. – megadriive Apr 20 '23 at 22:01
-
@MadProgrammer well more like reading a quora thing lmao. still reading up on the private keyword doe. – megadriive Apr 20 '23 at 22:03
-
@MadProgrammer omg i looked at the documentation you linked and i might've actually gotten somewhere, it's the vc events that hold the secret to actually assigning the values to a variable right? – megadriive Apr 20 '23 at 22:45
-
@MadProgrammer wait but it's like the vc event won't save to the variable, i can make it do something if i push a key but idk how to translate that into saving it to a variable doe. – megadriive Apr 20 '23 at 23:13
-
ok so i got it, all i needed to do was use an if statement where if a vc key is pressed, it'll do something (in my case, it adds to the array list) so for my example: `if (e.getKeyCode() == NativeKeyEvent.VC_W) { // typingDetector is my arraylist typingDetector.add(e.getKeyText(e.getKeyCode())); }` hopefully the code formats right – megadriive May 01 '23 at 00:09
-
well damn this code ain't newline correctly – megadriive May 01 '23 at 00:11