How to intercept music control keyboard shortcuts in Java? -
if keyboard has buttons play/pause/etc (music control shortcuts), , press them, itunes open (at least on mac).
if opened music player, spotify, intercept shortcut keys, , itunes won't anything.
well, want make music player java, , want have same behavior. want application intercept such shortcuts, , other programs shouldn't able interfere.
i using javafx, although don't think matters.
how can achieve this?
i able detect keys user presses using jnativehook, not know how intercept keys other applications won't things them.
once detect keys, send pause key song being played itunes paused, use boolean
variable detect between shortcuts being typed on keyboard or being send program(in case if need)
or
you use c code(start c program along java program) take @ @dave delongs answer on here modify nsevent send different key 1 pressed have different keyboard shortcut , modify c program send shortcut keys while itunes shortcut keys pressed, if need key codes where can find list of mac virtual key codes?
for example if music program uses p play songs
, r listen next song
, , itunes uses spacebar
play songs , right arrow key
go next one, modify @dave delongs answer here changes :-
#import <cocoa/cocoa.h> cgeventref mycgeventcallback(cgeventtapproxy proxy, cgeventtype type, cgeventref event, void *refcon) { //0x31 virtual keycode "spacebar" //0x23 virtual keycode "p" if (cgeventgetintegervaluefield(event, kcgkeyboardeventkeycode) == 0x31) { cgeventsetintegervaluefield(event, kcgkeyboardeventkeycode, 0x23); } //0x7c virtual keycode "right arrow" //0x0f virtual keycode "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); }
Comments
Post a Comment