I've been trying to do some kind of autoclicker and have the following code in java:
import java.awt.event.*;
import java.awt.*;
class keyStroke {
public void Execute() throws AWTException {
int n = 0;
while(n < 100){
Robot r = new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_1);
r.keyRelease(KeyEvent.VK_1);
++n;
}
}
}
It works pretty fine clicking the key 1, but, it doesn't work in some games.
It looks to be working only on chatbox and accessing to it (enter key), but aside from that, nothing else works (like using a skill or moving).
Then, I decided to also try in C++, with the following code
#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;
void SendKey (char Vk){
char VkKey = VkKeyScan(Vk);
keybd_event(VkKey, 0, 0, 0);
keybd_event(VkKey, 0, KEYEVENTF_KEYUP, 0);
}
int main(){
while(true){
SendKey('1');
Sleep(1000);
}
}
And the same thing happens.
What am U doing wrong? If the keypress doesn't work for this case I have to find something else?
I know from experience that some game input doesn't use an event based structure. Some games only check once every frame if a key is pressed. This means that your chance of pressing the key at that exact moment are zero.
Scripting utilities such as the logitech keyboard scripting tool face a similar problem and there it helps to have a delay between press and release.
Aside: chat windows usually have to use an input event as typing would be almost impossible if key presses are only registered once per frame.
Related
So, to give a bit of context, I finally got tired of having to research for the ALT codes of certain symbols and sub/superscript numbers, and never getting it right, I suppose this mapping error have something to do with de keyboard version I'm using, but to be honest, I don't care, and to solve this problem I created a program in Java that will print all the alt codes going from Alt0000 to Alt9999 on a separate notepad window via keyPress but, I'm having problems on printing all of them, somehow it erases the text multiple times during the run, and after a while it starts printing nonsensical things.
As requested in a comment bellow, here's a print of the nonsensical things:
Nonsensical Things
And for adding more details, the program runs fine for the first 100-ish codes, them it erases some of the codes and run fine until de 6000-ish codes, which is where it starts printing only the pre texts that indicate the codes and don't breaking any lines, a further more down the line it also erases the progress, and keeps with de "nonsensical things" as I am calling it.
If someone could give me some tips on how to do/fix this I would very much appreciate.
Since I'm not an expert programmer I'm also accepting tips on how to improve my code.
Thanks. :)
This is my code:
package randomProjects;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.*;
public class SimulateKeyPress {
public static void main(String[] args) throws InterruptedException {
int[] keyEvents = {KeyEvent.VK_NUMPAD0,
KeyEvent.VK_NUMPAD1,
KeyEvent.VK_NUMPAD2,
KeyEvent.VK_NUMPAD3,
KeyEvent.VK_NUMPAD4,
KeyEvent.VK_NUMPAD5,
KeyEvent.VK_NUMPAD6,
KeyEvent.VK_NUMPAD7,
KeyEvent.VK_NUMPAD8,
KeyEvent.VK_NUMPAD9};
try {
Robot robot = new Robot();
// click on notepad window positioned at the rigth of the IDE
robot.mouseMove(1000,400);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
// run trhought the thousand units
for(int um = 0; um <= 9; um++) {
// run trhought the hundreds
for(int c = 0; c <= 9; c++) {
// run trhought the dozens
for(int d = 0; d <= 9; d++) {
// run trhought the units
for(int u = 0; u <= 9; u++) {
// write the pressed keys
// alt+####
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_L);
robot.keyRelease(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_ADD);
robot.keyRelease(KeyEvent.VK_ADD);
robot.keyPress(keyEvents[um]);
robot.keyRelease(keyEvents[um]);
robot.keyPress(keyEvents[c]);
robot.keyRelease(keyEvents[c]);
robot.keyPress(keyEvents[d]);
robot.keyRelease(keyEvents[d]);
robot.keyPress(keyEvents[u]);
robot.keyRelease(keyEvents[d]);
robot.keyPress(KeyEvent.VK_TAB);
// write the respective ALT CODE result
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(keyEvents[um]);
robot.keyPress(keyEvents[c]);
robot.keyPress(keyEvents[d]);
robot.keyPress(keyEvents[u]);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(keyEvents[um]);
robot.keyRelease(keyEvents[c]);
robot.keyRelease(keyEvents[d]);
robot.keyRelease(keyEvents[u]);
// Break line
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
//Thread.sleep(100);
}
}
}
}
} catch (AWTException e) {
e.printStackTrace();
}
}
}
I've also found this related queries, but had no luck in finding an answer to my problem:
ALT-Codes in Java
How to detect Windows alt-code input without relying on keypress?
How to simulate keyboard presses in java?
I have a program which creates a window, loops through an array of colours and changes the window background to these colours.
I have used a pause method to allow me to pause each colour for a second before it loops onto the next.
Instead of pausing for a second, I want to be able to loop through the colours individually by pressing enter at each colour. I believe I need to make use of a scanner to do this, but I cannot get it to work myself.
Any help and an explanation would be really appreciated.
Here's the code that I've used to create the current pause method and loop the colours:
private void pause(long millisecs) {
long startTime = Calendar.getInstance().getTimeInMillis();
while(Calendar.getInstance().getTimeInMillis()-startTime<millisecs);
}
public void flashColour() {
Color [] rainbow = { Color.red,Color.orange,Color.yellow, Color.green,
Color.blue,Color.magenta,Color.black };
int index = 0;
System.out.println("Start");
pause(1000);
while(index < rainbow.length) {
getContentPane().setBackground(rainbow[index]);
pause(1000);
index++;
}
System.out.println("End");
}
I think you have misunderstood the purpose of a scanner. A scanner will parse primitive types and strings. What you're looking for is a keylistener. A keylistener will 'listen' for a specific key press (In this case enter) and run the appropriate code when that key is pressed.
For more information, see:
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
I plan on writing a solver to the recently popular 2048 game
github link
I'm wondering how I could go about this without actually building the game first then solving it... My question is: Is there a way I can send key presses (e.g. 'left' 'right' 'up' and 'down' ) into a web-browser via some sort of language like java/c?
Sorry if this question has been posted before, I was not sure how to actually phrase the question and could not find any results.
use keybd_event function to send key press,
example :
keybd_event(VK_UP,0xE0,0,0);//do click, it will be stay pressed until you release it
keybd_event(VK_UP,0xE0,KEYEVENTF_KEYUP,0);//release click
the second parameter is scan code,there is a list of make and break scan codes for each key
http://stanislavs.org/helppc/make_codes.html,
and here you can find the virtual key codes
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
Using Java applets you can add a text listener to your component and capture the Keystrokes. For example, in the code below you are capturing the keystrokes of a textbox.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class KeyReader extends Applet{
private static final long serialVersionUID = 1L;
public void init(){
TextField textBox = new TextField(" ");
add(textBox);
textBox.addKeyListener (new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println("You Pressed " + keyCode);
}
}
);
}
}
I would like to ask for a help with library OpenCV. I want to ask you if you know the best way how to detect a colored spot from picture. For example I need to create application which can calculate size of "dirty spot" on tshirt. Let's say that there is a brown tshirt and there is also a dirty spot made by katchup or by something else.
Could you recommend me algorithm or technics how to calculate it? Or some tutorial?
I wouldn't ask you for help but I am running out of time and perhaps you meet with that problem before.
Thank you very much.
In openCV samples codes (under cpp samples section), you can find a .cpp file named "bgfg_segm.cpp". Although that code is for motion tracking but i think that you can use to detect the spots also.
There by pressing the "spacebar key", you can start/stop updation of background. Once you have decided your background, then anything extra will be detected as a spot on it.
Strategy: Bring the cloth infront of webcam and once it is selected as a background, then press "spacebar key" to stop further changes in the background. Then, your program should be able to track any change in the color of your cloth.
The code is below:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap;
bool update_bg_model = true;
cap.open(0);
namedWindow("image", CV_WINDOW_NORMAL);
namedWindow("foreground mask", CV_WINDOW_NORMAL);
namedWindow("foreground image", CV_WINDOW_NORMAL);
namedWindow("mean background image", CV_WINDOW_NORMAL);
// Declare "object " of class "BackgroundSubtractorMOG2"
BackgroundSubtractorMOG2 bg_model;//(100, 3, 0.3, 5);
Mat img, fgmask, fgimg;
for(;;)
{
cap >> img;
if( fgimg.empty() )
fgimg.create(img.size(), img.type());
//update the model
bg_model(img, fgmask, update_bg_model ? -1 : 0); // "bg_model" is object of class "BackgroundSubtractorMOG2" as declared above.
fgimg = Scalar::all(0);
img.copyTo(fgimg, fgmask);
Mat bgimg;
bg_model.getBackgroundImage(bgimg);
imshow("image", img);
imshow("foreground mask", fgmask);
imshow("foreground image", fgimg);
if(!bgimg.empty())
imshow("mean background image", bgimg );
char k = (char)waitKey(30);
if( k == 27 ) break;
if( k == ' ' ) // Change the Background updation status by Spacebar key
{
update_bg_model = !update_bg_model; // initially "bool update_bg_model = true"
if(update_bg_model)
printf("Background update is on\n");
else
printf("Background update is off\n");
}
}
return 0;
}
I'm trying to control some Java game from FireFox window. How can I send key and mouse events to that Java applet?
I'm using Windows XP if that matters.
Edit: I'm not trying to do this with Java even though i have the tag here. A c++ solution would be optimal.
You might try using Robot, but this might not work in FireFox. You can also use methods like abstractbutton.doClick()
If Robot doesn't work, key events you can synthesize by just setting text on a component, and mouse events you can use doClick() and requestFocus()
If none of that works, you might be able to accomplish your goals working with javascript and an html page.
Here is something that will work for keystrokes:
The recommended methods for both these actions are using SendInput
This website is perfect for beginning to understand sendinput
To find windows targets use Spy++, documentation
but I do have other examples below:
Example here is for Notepad using postmessage.
#include "TCHAR.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
HWND hwndWindowTarget;
HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
if (hwndWindowNotepad)
{
// Find the target Edit window within Notepad.
hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
if (hwndWindowTarget) {
PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
}
}
return 0;
}
You may also like to look at windows hooks, which can send mouse input
Or User32 mouse_event:
[DllImport("User32.Dll")]
private static extern void mouse_event(UInt32 dwFlags, int dx, int dy, UInt32 dwData, int dwExtraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public enum MouseEventFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public static void SendLeftClick(int X, int Y)
{
mouse_event((uint)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0);
mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
}