How to intercept music control keyboard shortcuts in Java? - java

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.

Related

How to Automate Keypress "Space" and Arrow Keys in SAP using Java/AutoIT

I am using Java/AutoIT. I need to automate the keypress of "Space" and "Down_Arrow_Key" keys when automating in SAP. I have already done for "Enter" using the below code but I cannot find a way to automate for "Space" and "Down_Arrow_Key".
public void pressEnter() {
this.findElement("wnd[0]").invoke("sendVKey", 0);
}
So, my question is how to automate "Space" and "Down_Arrow_Key" keypresses when automating in SAP Desktop Application, when the element tracking is done via AutoIT(version 3)?
I'm not sure I understand why can't you use regular .sendKeys() method here.
With it you can simply send Space and Arrow Down keys as following:
driver.findElement(By.id("your_element_id")).sendKeys(Keys.SPACE);
driver.findElement(By.id("your_element_id")).sendKeys(Keys.ARROW-DOWN);
This can also be done with Actions:
Actions action = new Actions(Driver);
action.SendKeys(Keys.SPACE).Build().Perform();
action.SendKeys(Keys.ARROW_DOWN).Build().Perform();
But if you still have to use the .invoke() method looks like this should work:
Method objTest = WebDriver.class.getMethod(strMethod, CharSequence.class);
objTest.invoke(strMethod, Keys.SPACE);
objTest.invoke(strMethod, Keys.ARROW_DOWN);
As described here

Calling the voice-input feature - CodenameOne app (iOS port)

My Android app features a text input box that has a button on the right of the EditText to call the voice-input feature.
I am porting the app with Codename One. At present time the iOS port is the goal.
The button has a suitable icon. This is the code:
voiceInputButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
try {
activity.startActivityForResult(voiceIntent, RESULT_SPEECH_REQUEST_CODE);
} catch (ActivityNotFoundException ex) {
}
}
});
It works very well, the voice-input screen is called and then the result is passed back to the app as a string.
The string is what the user said (for example, a single word).
I need to have this functionality in the CodenameOne app for iOS.
What should be the equivalent? Is it necessary to call native iOS functions, through the native interface?
You can implement speech-to-text via Speech framework, to perform speech recognition on live or prerecorded audio. More info: https://developer.apple.com/documentation/speech
About Codename One, you can create a native interface using Objective-C code.
To use the Speech framework with Objective-C, see this answer:
https://stackoverflow.com/a/43834120
The answer says so: «[...] To get this running and test it you just need a very basic UI, just create an UIButton and assign the microPhoneTapped action to it, when pressed the app should start listening and logging everything that it hears through the microphone to the console (in the sample code NSLog is the only thing receiving the text). It should stop the recording when pressed again. [...]». This seems very close to what you asked.
Obviously the creation of the native interface takes time. For further help, you can ask more specific questions, I hope I have given you a useful indication.
Lastly, there are also alternative solutions, again in Objective-C, such as: https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/objectivec/ios/from-microphone
You can search on the web for: objective-c speech-to-text

How to know the drop object in DropTargetDragEvent Java Swing comes from Outlook

In a drop event (DropTargetDropEvent) in Java Swing which files,emails and others can be drop into a drop target, I would like to know if the drop object is an email from outlook. In the code below both file and email can enter the "if" condition.
public void drop(java.awt.dnd.DropTargetDropEvent dropEvt )
{
Transferable transferable = dropEvt.getTransferable();
if(transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
{
//Dropped files or email from Outlook enters this condition
}
}
Thanks!
I saw the post below which answers my question, need to get the active window information on the dragEnter event method.
Getting active window information in Java

Android Global KeyListener necessary?

I want my app to start, after the back button was pressed 3Times.
So I created this class which is executed in the Launcher.
public class KeyManager extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent == "Back key"){
Log.e("KM", "PRESSED!!!!!"+keyCode);
}
//return super.onKeyDown(keyCode, event);
return false;
}
}
This code works, if it s implemented in the launcher class, but it does not work, if the app is in the background. How or which key listener do I need for listening to background keys or more if the app is working only in the background?
This is not possible due security reasons. It's more likely a keylogger. If you really want to do that use an AccessibilityService which let you capture the KeyHooks even on a non-rooted Device. Warning: This is Dangerous and user must explizit enable it, to get it activated.
You may/can also modify the default keyboard, to capture those.
I want my app to start, after the back button was pressed 3Times.
You are welcome to download the Android source code, alter the operating system to contain this feature, compile it into a ROM mod, and install that ROM mod on your device.
Otherwise, what you want is not supported by Android.

IPhone password field in AWT/SWT?

I want to create a special Password Dialog for my eclipse product, which is used with an on screen keyboard.
It would be very nice, if i could use a component like the IPhone Password field. In this field, the added character is shown for a second and after the second it is converted into the '*' character for hiding the complete password.
Did a jar/library exists, this is implemented in AWT or SWT?
Edit:
I could trying to implement it from scratch (SWT), but for these i would have to create a very special and complicated KeyListener for the password Text component. I would have to catch the keyReleased event and set the characters manually into the field.
So far i was not able to find any libraries in the web. Suggestion how this can be implemented are welcome too.
This is not really a full answer, rather than a discussion starter and I don't know of any out-of-the-box widgets which can do that.
My first idea was to inheriting the swt Text widget and overriding setEchoChar et al., but after looking at the code this doesn't really seem feasible, because this method is merely a wrapper around:
OS.SendMessage (handle, OS.EM_SETPASSWORDCHAR, echo, 0);
If anyone would know the OS specific low-level implementation, that might be helpful.
Anyway, on to a different approach. I would avoid the KeyListener and use a ModifyListener on the Text-Widget.
void addModifyListener(ModifyListener listener)
You could then build a wrapper which catches the entered text using this listener, appends it to a locally held string/stringbuffer (or e.g. the Eclipse Preferencestore) and send a modified full text to the Text widget using setText(String s), replacing all characters except the last by an echo character (e.g. *).
myText.setText((s.substring(0, s.length()-1)).replaceAll("[\\s\\S]","*")+s.charAt(s.length()-1));
This is a bit of a kludge, but it should work.
The not so straightforward bit is the 1 second timing, without stalling the whole view...
Depending on what Jules said the following code is some kind of working.
The code is quick and fast and i would like to have a more thread safe solution.
originalString = new StringBuffer();
passwordField.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
synchronized (passwordField) {
String s = passwordField.getText();
String newS = s.replaceAll("[\\s\\S]", "*");
if (newS.equals(s)) {
while (originalString.length() > s.length()) {
originalString = originalString.deleteCharAt(originalString.length() - 1);
}
usernameField.setText(originalString.toString());
return;
}
if (originalString.length() < s.length()) {
originalString.append(s.charAt(s.length() - 1));
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
}
passwordField.setText(newS);
}
passwordField.redraw();
passwordField.setSelection(passwordField.getText().length());
}
});
Key Events are cached, so you can add more characters, also when the Thread is waiting.
Another Problem is the Cursor handling. the Cursor always moves to the first position, when you set the Text.
I think when this is working it is very near to the iphone solution.

Categories