Highlighted

[SCRIPTING] Get Keys combinations (Modificators + NumPad)

Enthusiast ,
Aug 16, 2020

Copy link to clipboard

Copied

Hello, there.

Scripting question.

Any way to get key modificators + num pad presses in a edittext field?

I need to get Ctrl/Shift/Alt + Numpad numbers for both Mac and Windows.

Is it possible directly in javascript?

 

I'm trying something like this...

I also tried combination of keydown and keyup. 

 

 

function main() {
	var w = new Window("dialog" , "Script by LFCorullón");
	var e = w.add("edittext" , [0,0,140,24] , "");
		
	e.addEventListener("keyup" , keypress);
	function keypress(k) {
		str = [];
		if (e.altKey) str.push("Alt");
		if (e.ctrlKey) str.push("Ctrl");
		if (e.shiftKey) str.push("Shift");
		if (e.metaKey) str.push("Win");
		str.push(k.keyName);
		alert(str);
		}
	w.show();
	}

 

 

TOPICS
Scripting

Views

124

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

[SCRIPTING] Get Keys combinations (Modificators + NumPad)

Enthusiast ,
Aug 16, 2020

Copy link to clipboard

Copied

Hello, there.

Scripting question.

Any way to get key modificators + num pad presses in a edittext field?

I need to get Ctrl/Shift/Alt + Numpad numbers for both Mac and Windows.

Is it possible directly in javascript?

 

I'm trying something like this...

I also tried combination of keydown and keyup. 

 

 

function main() {
	var w = new Window("dialog" , "Script by LFCorullón");
	var e = w.add("edittext" , [0,0,140,24] , "");
		
	e.addEventListener("keyup" , keypress);
	function keypress(k) {
		str = [];
		if (e.altKey) str.push("Alt");
		if (e.ctrlKey) str.push("Ctrl");
		if (e.shiftKey) str.push("Shift");
		if (e.metaKey) str.push("Win");
		str.push(k.keyName);
		alert(str);
		}
	w.show();
	}

 

 

TOPICS
Scripting

Views

125

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 16, 2020 0
Adobe Community Professional ,
Aug 17, 2020

Copy link to clipboard

Copied

I think you need to be looking at the keypress ("k") not the edittext field ("e"), unless I'm missing what you're going after: 

 

function main() {
	var w = new Window("dialog" , "Script by LFCorullón");
	var e = w.add("edittext" , [0,0,140,24] , "");
		
	e.addEventListener("keyup" , keypress);
	function keypress(k) {
		str = [];
		if (k.altKey) str.push("Alt");
		if (k.ctrlKey) str.push("Ctrl");
		if (k.shiftKey) str.push("Shift");
		if (k.metaKey) str.push("Win");
		str.push(k.keyName);
		alert(str);
		}
	w.show();
   }
   
   main();

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 17, 2020 1
Enthusiast ,
Aug 17, 2020

Copy link to clipboard

Copied

Wow. Thank you so much, again, Brian!

Any idea on why some combination + numpad shows only one of the modifier key?

 

I want to capture modificators + numpad 0-9.

And then fill the edittext field with the array joined with "+".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 17, 2020 0
Enthusiast ,
Aug 17, 2020

Copy link to clipboard

Copied

Almost there...

Now, I just need to restrict to numpad and make the shift key work.

The shift key is working with the numbers on top of keyboard, but not with numpad numbers.

 

function main() {
	var w = new Window("dialog" , "Script by LFCorullón");
	var e = w.add("edittext" , [0,0,140,24] , "");
	var v = w.add("edittext" , [0,0,140,24] , "");
		
	e.addEventListener("keydown" , keypress);
	function keypress(k) {
		str = [];
		if (k.ctrlKey) str.push("Ctrl");
		if (k.altKey) str.push("Alt");
		if (k.metaKey) str.push("Win");
		if (k.shiftKey) str.push("Shift");
		
		e.addEventListener("keyup" , keyup);
		function keyup(k) {
			if (k.keyName >= 0 && k.keyName <= 9) {
				str.push(k.keyName);
				e.text = "";
				e.text = str.join("+");
				}
			}
		}
	
	w.show();
   }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 17, 2020 0
Adobe Community Professional ,
Aug 18, 2020

Copy link to clipboard

Copied

I'm not sure. I don't have a numpad. But this revised script is displaying "Shift+3+3+3" if I press Shift as well as 3 when I run it on my Mac, FYI. You might want to look into running a VBS code snippet for the keylogger, which might behave better than using the KeyUp/KeyDown event listeners, unless someone else has a better idea. Here's something that might help. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 18, 2020 0
Enthusiast ,
Aug 18, 2020

Copy link to clipboard

Copied

The problem is I want this working on both Mac and Windows. I'm trying this approach exactly because of that. If is Windows only, I already have an awesome solution with jsx + ahk (autohotkey). 

 
Any idea will be appreciated. Thanks. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 18, 2020 0
Enthusiast ,
Aug 22, 2020

Copy link to clipboard

Copied

Hello, there.

Any other suggestions on this?

 

My current situation:

- Shift+numpad numbers: captures only the number
- Shift+keyboard top numbers: captures both (shift and the number)
- Shift combinaded with Ctrl or Alt and numpad numbers: shift is ignored
- Shift combinaded with Ctrl or Alt and keyboard top numbers: all captured

- The Alt+Number alert I set only works with numpad numbers

 

I'm using Windows 10 and InDesign CC2020 and CC2021 (same behavior).

I tried the "keyup" event listener inside and outside the "keydown" event listener. Same result on both sides.

 

Thank you so much. I'll really appreciate any help!

 

main();

function main() {
	var w = new Window("dialog" , "Script by LFCorullón");
	var e = w.add("edittext" , [0,0,140,24] , "");
	e.active = true;
	
	e.addEventListener("keydown" , keypress);
	
	function keypress(k) {
		str = [];
		if (k.ctrlKey) str.push("Ctrl");
		if (k.altKey) str.push("Alt");
		if (k.metaKey) str.push("Win");
		if (k.shiftKey) str.push("Shift");
		
		e.addEventListener("keyup" , keyup);
		function keyup(k) {
			if (k.keyName >= "0" && k.keyName <= "9") {
				str.push(k.keyName);
				if (str.join("+").match(/\d\+\d$/gi)) str.splice(-1 , 1);
				e.text = "";
				e.text = str.join("+");
				}
			}

		}
	
	e.onChanging = e.onChange = function () {
		if (e.text.match(/\d\+\d$/gi)) {
			e.text = e.text.replace(/\+\d$/gi , "");
			}
		if (e.text.match(/^.(?=(alt|shift|ctrl))/gi)) {
			e.text = e.text.replace(/^(.)(alt|shift|ctrl)/gi , "$2");
			}
		if (e.text.match(/^alt\+\d$/gi)) {
			e.text = "";
			alert("Alt key must be used in conjunction\rwith another modifier key (Ctrl or Shift)." , "Script by LFCorullón" , true);
			}
		}
	
	w.show();
   }

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 22, 2020 0