I am pressing some keys on keyboard(and buttons on mouse) remotely using socket and java Robot class.
Homebrew program is running on my Nintendo Wii.(but in here, I am using emulator)
Wii sends Wii remote button info through socket, and java program receives it and presses key or controls mouse.
Everything seems working well, except for Windows key.
It receives key press as well as home button.(and PC should open windows menu when I press home button)
So, I think the problem is in pressing key.
I press the key using this method.
if Press is true It presses the key. Otherwise, It releases the key.
private static void PressKey(Robot R, int Key, boolean Press)
{
if(Press)
R.keyPress(Key);
else
R.keyRelease(Key);
}
And I called like this:
PressKey(rBot, KeyEvent.VK_WINDOWS, BH);
rBot is:
Robot rBot = new Robot();
BH is true when home button is pressed, false when home button is not pressed
How can I fix this?
Related
If your keyboard has buttons for play/pause/etc (music control shortcuts), and you press them, iTunes will open (at least on Mac).
If you recently opened another music player, like Spotify, it will actually intercept the shortcut keys, and iTunes won't do anything.
Well, I want to make a music player with Java, and I want to have the same behavior. I want my application to intercept such shortcuts, and other programs shouldn't be able to interfere.
I am using JavaFX, although I don't think that really matters.
How can I achieve this?
I am already able to detect they keys the user presses using JNativeHook, but I do not know how to intercept the keys so that other applications won't do things with them.
Once you detect the keys, you could send the pause key so that the song that is being played by itunes is paused, you could use a boolean variable to detect between the shortcuts being typed on the keyboard or being send by the program(in case if you need)
or
You could use some c code(start the c program along with your java program) take a look at #Dave Delongs answer over here Modify NSEvent to send a different key than the one that was pressed
You could have a different keyboard shortcut and modify the c program to send your shortcut keys while the Itunes Shortcut keys are pressed, if you need the key codes Where can I find a list of Mac virtual key codes?
for example if your music program uses p to play songs and r to listen to the next song, and itunes uses spacebar to play songs and right arrow key to go to the next one, you could do modify #Dave Delongs answer here are the changes :-
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x31 is the virtual keycode for "Spacebar"
//0x23 is the virtual keycode for "p"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x31) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x23);
}
//0x7C is the virtual keycode for "Right arrow"
//0x0F is the virtual keycode for "R"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x7C) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x0F);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
You may be able to use some of the code from iTunesPatch to accomplish what you're looking for, but it appears that a system daemon may need to be modified upon installation, and you will likely have to use Objective-C/Swift.
There are further details about iTunesPatch in a blog post here.
I'm trying to open a link in a new window using selenium with Chrome driver. I'd like to use keyDown to hold SHIFT and "w" while clicking the webelement. These attempts have not worked:
Actions act = new Actions(driver);
Action series = act.keyDown(englishButton, Keys.SHIFT).keyDown(englishButton, "w").click(englishButton).build();
series.perform();
The ".keyDown(englishButton, "w")" portion gives me an error there. And I've also tried using java robot:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_W);
englishButton.click();
but after importing Robot, KeyEvent wasn't recognizing VK_SHIFT or VK_W as valid entries.
First, in Google Chrome, to open a link in a new tab you don't need to hold down W, just SHIFT.
Second, .keyDown(englishButton, "w") shouldn't compile because it takes an org.openqa.selenium.Keys instance (not a char). If you want to send characters you should use Actions.sendKeys(CharSequence...) (it doesn't release the modifier keys unlike WebElement.sendKeys(CharSequence...)):
Action series = act.sendKeys(Keys.SHIFT, "w").click(englishButton).keyUp(Keys.SHIFT).build()
This should first key-down on SHIFT+W, then click your button/link, and then release SHIFT (as W isn't a modifier key you don't need to release it).
And if you don't care about holding down W but just SHIFT then you can try the following:
Action series = act.keyDown(Keys.SHIFT).click(englishButton).keyUp(Keys.SHIFT).build()
This should simply key-down SHIFT, click your button/link, and then release SHIFT.
"w" button should be "pressed" using sendKeys() method. But as #mfulton26 has mentioned, there is no need to do it if you want to open a new tab in Chrome. Also there is no need to use englishButton as a first argument in keyDown() or keyUp() methods. Please take a look:
Actions act = new Actions(driver);
Action series = act.
keyDown(Keys.SHIFT). // Press Shift key
click(englishButton). // Click the link
keyUp(Keys.SHIFT). // Release Shift key
build(); // Build the chain of actions
series.perform();
Hope this helps.
I have the following problem: I am working on a Java Swing application that show me a JFrame.
What I have to do is that when the user click on the X button the window have to be iconified and not close (it is a requirement requested by the client).
So I have a MainFrame class that extends a clssic JFram of Swing.
The application run on Linux Ubuntu and another requirement of the client is the following one: when the user do right click on the application icon in the Unity menu bar, and then click on the quit voice the application must shut down
My problem is that I can iconize the application when the user click on the X button changing this property:
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
in this way:
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then I add a WindoListener that say that my mainFrame object (my window) have to be iconified when the user click on the X button.
mainFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
logger.info("Minimize the MainFrameWindows instead close it");
((MainFrame)e.getSource()).setState(MainFrame.ICONIFIED);
logger.info("EVENTO ICONIZZAZIONE: " + e.getSource().toString());
}
});
Ok, this work pretty well...the problem is that the same method will be called also if I click on the quit button on the application icon in the Ubuntu Unity main menu. So I obtain the same behavior.
My problem is that I would 2 different behavior:
1) Minimize the application in the menu if the user click on the X button in the application
2) Shut down the application if the user click on the quit button of the application contextual menu in the Ubuntu Unity main menu
Do you have some ideas about how do it?
Tnx
Andrea
Maybe something like:
#Override
public void windowClosing(WindowEvent e) {
JFrame f = (JFrame) e.getWindow();
if (f.getState() != JFrame.ICONIFIED)
f.setState(JFrame.ICONIFIED);
else
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I am unable to press 'Go' button on searching something on Nexus 7' tablet. We don't have any text or content description for the 'Go' button on the keyboard. I tried using following -
//Search something say "fun"
new UiObject(new UiSelector().text("Enter URL or Search & Win")).setText("fun");
getUiDevice().pressEnter();
OR
getUiDevice().pressSearch();
Also tried :
getUiDevice().pressKeyCode(66); //for enter
getUiDevice().pressKeyCode(84); // for search
But this is not working.
Could anyone help me out with this.
Thanks
Try using the button attribute with reference to index.
i.e :
UiObject cancelButton = new UiObject(new UiSelector().className("android.widget.Button"));
To click on "Done" button with UIAutomator just try below code
use
just make sure that correct layout in which input keyboard is open is used
UiObject(new UiSelector().resourceId(LAYOUTID)).clickBottomRight();
I have a requirement to restrict the user from pressing back button or disabling the back button in a screen. How should I get the Task done?
And Also on the same screen if user clicks the Ok button all the Screens from Home should get cleared and Home Screen should be displayed.
I got an answer here but it doesn't work. I am testing the app on Simulator 9550. Don't whether it is OS issue.
Thanks.
In order to modify the behaviour when the user presses ESC / back, you simply override the keyChar() method in your Screen subclass(es):
protected boolean keyChar(char c, int status, int time) {
if (c == Characters.ESCAPE) {
// do nothing if ESC was pressed
return true;
} else {
// accept the default behaviour for other keys
return super.keyChar(c, status, time);
}
}
In order to pop (remove) all screens except the app's home screen see this recent answer ... the one you linked to has a bug in it.