Old 01-10-2019, 03:34 PM   #1
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default Detect keyboard input in Lua script without GUI

Hi

Is it possible to get a Lua script to react to keyboard presses, but without a GUI?

So, for example, run a script and once it's running, if I press one key it does one thing, and if I press another key it does something else, but without a dialogue box opening, or having to confirm the key press with an OK button or the Enter key etc?

So, for example, if I held down a key on the keyboard, it would repeat the code for that key (at the kbd rpt rate) until I let go....

Thanks
Andy
andyp24 is offline   Reply With Quote
Old 01-10-2019, 07:55 PM   #2
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default

You can try juliansander's API extension. But only the modifier keys and mouse buttons can be detected.
https://forum.cockos.com/showthread.php?t=212174
dsyrock is offline   Reply With Quote
Old 01-11-2019, 12:38 AM   #3
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Thanks for the replies....

To clarify, I mean the computer keyboard not a MIDI keyboard.

Andy
andyp24 is offline   Reply With Quote
Old 01-11-2019, 06:45 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

I've suggested it to Julian, and he replied that he couldn't figure out, how to catch keyboard-inputs sent to Reaper-windows, yet.
So I think, it is impossible, at least for now.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 01-11-2019, 07:03 AM   #5
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Keyboard input can be intercepted globally (within REAPER) using accelerator_register_t from the C++ extension API.



https://gist.github.com/cfillion/d86...fd7b2ea82f7ff6

Last edited by cfillion; 01-11-2019 at 07:24 AM.
cfillion is offline   Reply With Quote
Old 01-11-2019, 07:21 AM   #6
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Thanks again.

I'd have literally NO idea how to use that C++ thing within a Lua script, and I wouldn't be at all surprised to hear it's impossible.

I'm really quite new to this - the last time I wrote any computer code was at University in the 1980s!!

Doing OK with Lua for now, and managing to make some scripts that are really useful in my workflow, but nothing too complex :-)

The reason for asking this was to see if it might help with the Undo problem discussed here:

https://forum.cockos.com/showthread.php?t=211932

(ie if a script could intercept keyboard commands then it could continue performing an action as the key was repeatedly pressed, then after a certain "timeout" of no presses, it could exit and make an Undo point).

But it sounds like it's a blind alley.
andyp24 is offline   Reply With Quote
Old 01-11-2019, 08:32 AM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Quote:
Originally Posted by cfillion View Post
Keyboard input can be intercepted globally (within REAPER) using accelerator_register_t from the C++ extension API.



https://gist.github.com/cfillion/d86...fd7b2ea82f7ff6
Oh, that's great

How difficult would it be to add it to Julian's plugin?

@andyp24
You can't use C++-stuff directly from Lua, but if someone adds that as a function into an extension, you could use it, like any other ReaScript-API-function.
You would just need to install that extension that contains this new function.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 01-11-2019, 09:50 AM   #8
andyp24
Human being with feelings
 
andyp24's Avatar
 
Join Date: Mar 2016
Posts: 1,239
Default

Thanks Mespotine, I almost understood that ;-)

Andy
andyp24 is offline   Reply With Quote
Old 10-26-2022, 11:04 PM   #9
binbinhfr
Human being with feelings
 
binbinhfr's Avatar
 
Join Date: Oct 2021
Location: France
Posts: 363
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
You can't use C++-stuff directly from Lua, but if someone adds that as a function into an extension, you could use it, like any other ReaScript-API-function.
You would just need to install that extension that contains this new function.
Anything new on this side ?
I am still wondering if an unfocused defer app can detect computer keyboard inputs ? (apparently gfx.getchar does not)
__________________
Reaper's lunatic
Reapack repository / GitHub / SoundCloud / Donate
binbinhfr is offline   Reply With Quote
Old 10-26-2022, 11:09 PM   #10
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

JS-extension can do it, but I didn't understand, how it works.
Look into the JS-extension-thread for more information on this...
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 10-26-2022, 11:40 PM   #11
binbinhfr
Human being with feelings
 
binbinhfr's Avatar
 
Join Date: Oct 2021
Location: France
Posts: 363
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
JS-extension can do it, but I didn't understand, how it works.
Look into the JS-extension-thread for more information on this...
oh I see : JS_VKeys_GetState, JS_VKeys_GetDown and so on.

I'll have a look and see how it works.

Thanks Mespotine.
__________________
Reaper's lunatic
Reapack repository / GitHub / SoundCloud / Donate
binbinhfr is offline   Reply With Quote
Old 10-27-2022, 03:39 AM   #12
binbinhfr
Human being with feelings
 
binbinhfr's Avatar
 
Join Date: Oct 2021
Location: France
Posts: 363
Default

So it seems to work,
but I have interrogations :
- does it work only on windows ?
- does it work on any "local" keyboard (qwerty, azerty)

And it works even if the script is not focused ! (contrary to gfx.getchar())
Note that the JS_VKeys_Intercept function behaves sometimes strangely:
it allows to block the transmission of the captured key to Reaper, which can be nice,
except (at least) the SPACE key (PLAY that might be more hardcoded ?).

-------

So here is my test code (needs JS-extension).
If a MAC and/or LINUX user can test it, it would be nice
And also a WINDOW user with QWERTY keyboard (I have AZERTY)

Please Type a, z, e, a, s, d, z, x, c, F1, F12, and these special keys that are local-specific (;:!,<*ù$^=²)

And post a copy of the console text, with eventually comments on special keys, so that I can recognize where they are on your keyboard...

Code:
----------------------------------------------------------------------------------------------------------
key_low, key_hi = 8, 0xFE --0xA5 -- Range of virtual key codes to check for key presses
keys_zero = string.rep("\0", key_hi-key_low+1) -- For quick comparison to zero state
keys_state = "" -- states of key currently pressed
keys_down = "" -- keys pressed since last call
keys_prev = ""
time_keys_prev = 0.0

KEYCODES = {
  VK_LBUTTON 	    = 0x01,   --  The left mouse button
  VK_RBUTTON 	    = 0x02,   --  The right mouse button
  VK_CANCEL 	    = 0x03,   --  The Cancel virtual key, used for control-break processing
  VK_MBUTTON 	    = 0x04,   --  The middle mouse button
  VK_BACK 	      = 0x08,   --  Backspace
  VK_TAB 	        = 0x09,   --  Tab
  VK_CLEAR 	      = 0x0C,   --  5 (keypad without Num Lock)
  VK_ENTER   	    = 0x0D,   --  Enter
  VK_SHIFT 	      = 0x10,   --  Shift (either one)
  VK_CONTROL 	    = 0x11,   --  Ctrl (either one)
  VK_MENU         = 0x12,   --  Alt (either one)
  VK_PAUSE        = 0x13,   --  Pause
  VK_CAPITAL 	    = 0x14,   --  Caps Lock
  VK_ESCAPE 	    = 0x1B,   --  Esc
  VK_SPACE 	      = 0x20,   --  Spacebar
  VK_PAGEUP 	    = 0x21,   --  Page Up
  VK_PAGEDOWN 	  = 0x22,   --  Page Down
  VK_END 	        = 0x23,   --  End
  VK_HOME 	      = 0x24,   --  Home
  VK_LEFT 	      = 0x25,   --  Left Arrow
  VK_UP 	        = 0x26,   --  Up Arrow
  VK_RIGHT 	      = 0x27,   --  Right Arrow
  VK_DOWN 	      = 0x28,   --  Down Arrow
  VK_SELECT 	    = 0x29,   --  Select
  VK_PRINT 	      = 0x2A,   --  Print (only used by Nokia keyboards)
  VK_EXECUTE 	    = 0x2B,   --  Execute (not used)
  VK_SNAPSHOT 	  = 0x2C,   --  Print Screen
  VK_INSERT 	    = 0x2D,   --  Insert
  VK_DELETE 	    = 0x2E,   --  Delete
  VK_HELP 	      = 0x2F,   --  Help
  VK_0 	          = 0x30,   --  0
  VK_1 	          = 0x31,   --  1
  VK_2 	          = 0x32,   --  2
  VK_3 	          = 0x33,   --  3
  VK_4 	          = 0x34,   --  4
  VK_5 	          = 0x35,   --  5
  VK_6 	          = 0x36,   --  6
  VK_7 	          = 0x37,   --  7
  VK_8 	          = 0x38,   --  8
  VK_9 	          = 0x39,   --  9
  VK_A 	          = 0x41,   --  A
  VK_B 	          = 0x42,   --  B
  VK_C 	          = 0x43,   --  C
  VK_D 	          = 0x44,   --  D
  VK_E 	          = 0x45,   --  E
  VK_F 	          = 0x46,   --  F
  VK_G 	          = 0x47,   --  G
  VK_H 	          = 0x48,   --  H
  VK_I 	          = 0x49,   --  I
  VK_J 	          = 0x4A,   --  J
  VK_K 	          = 0x4B,   --  K
  VK_L 	          = 0x4C,   --  L
  VK_M 	          = 0x4D,   --  M
  VK_N 	          = 0x4E,   --  N
  VK_O 	          = 0x4F,   --  O
  VK_P 	          = 0x50,   --  P
  VK_Q 	          = 0x51,   --  Q
  VK_R 	          = 0x52,   --  R
  VK_S 	          = 0x53,   --  S
  VK_T 	          = 0x54,   --  T
  VK_U 	          = 0x55,   --  U
  VK_V 	          = 0x56,   --  V
  VK_W 	          = 0x57,   --  W
  VK_X 	          = 0x58,   --  X
  VK_Y 	          = 0x59,   --  Y
  VK_Z 	          = 0x5A,   --  Z
  VK_STARTKEY 	  = 0x5B,   --  Start Menu key
  VK_CONTEXTKEY 	= 0x5D,   --  Context Menu key
  VK_NUMPAD0 	    = 0x60,   --  0 (keypad with Num Lock)
  VK_NUMPAD1 	    = 0x61,   --  1 (keypad with Num Lock)
  VK_NUMPAD2 	    = 0x62,   --  2 (keypad with Num Lock)
  VK_NUMPAD3 	    = 0x63,   --  3 (keypad with Num Lock)
  VK_NUMPAD4 	    = 0x64,   --  4 (keypad with Num Lock)
  VK_NUMPAD5 	    = 0x65,   --  5 (keypad with Num Lock)
  VK_NUMPAD6 	    = 0x66,   --  6 (keypad with Num Lock)
  VK_NUMPAD7 	    = 0x67,   --  7 (keypad with Num Lock)
  VK_NUMPAD8 	    = 0x68,   --  8 (keypad with Num Lock)
  VK_NUMPAD9 	    = 0x69,   --  9 (keypad with Num Lock)
  VK_MULTIPLY 	  = 0x6A,   --  * (keypad)
  VK_ADD 	        = 0x6B,   --  = 0x(keypad)
  VK_SEPARATOR 	  = 0x6C,   --  Separator (never generated by the keyboard)
  VK_SUBTRACT 	  = 0x6D,   --  - (keypad)
  VK_DECIMAL 	    = 0x6E,   --  . (keypad with Num Lock)
  VK_DIVIDE 	    = 0x6F,   --  / (keypad)
  VK_F1 	        = 0x70,   --  F1
  VK_F2 	        = 0x71,   --  F2
  VK_F3 	        = 0x72,   --  F3
  VK_F4 	        = 0x73,   --  F4
  VK_F5 	        = 0x74,   --  F5
  VK_F6 	        = 0x75,   --  F6
  VK_F7 	        = 0x76,   --  F7
  VK_F8 	        = 0x77,   --  F8
  VK_F9 	        = 0x78,   --  F9
  VK_F10 	        = 0x79,   --  F10
  VK_F11 	        = 0x7A,   --  F11
  VK_F12 	        = 0x7B,   --  F12
  VK_F13 	        = 0x7C,   --  F13
  VK_F14 	        = 0x7D,   --  F14
  VK_F15 	        = 0x7E,   --  F15
  VK_F16 	        = 0x7F,   --  F16
  VK_F17 	        = 0x80,   --  F17
  VK_F18 	        = 0x81,   --  F18
  VK_F19 	        = 0x82,   --  F19
  VK_F20 	        = 0x83,   --  F20
  VK_F21 	        = 0x84,   --  F21
  VK_F22 	        = 0x85,   --  F22
  VK_F23 	        = 0x86,   --  F23
  VK_F24 	        = 0x87,   --  F24
  VK_NUMLOCK 	    = 0x90,   --  Num Lock
  VK_OEM_SCROLL 	= 0x91,   --  Scroll Lock
  VK_OEM_1 	      = 0xBA,   --  ;
  VK_OEM_PLUS 	  = 0xBB,   --  =
  VK_OEM_COMMA 	  = 0xBC,   --  ,
  VK_OEM_MINUS 	  = 0xBD,   --  -
  VK_OEM_PERIOD 	= 0xBE,   --  .
  VK_OEM_2 	      = 0xBF,   --  /
  VK_OEM_3 	      = 0xC0,   --  `
  VK_OEM_4 	      = 0xDB,   --  [
  VK_OEM_5 	      = 0xDC,   --  \
  VK_OEM_6 	      = 0xDD,   --  ]
  VK_OEM_7 	      = 0xDE,   --  '
  VK_OEM_8 	      = 0xDF,   --  (unknown)
  VK_ICO_F17 	    = 0xE0,   --  F17 on Olivetti extended keyboard (internal use only)
  VK_ICO_F18 	    = 0xE1,   --  F18 on Olivetti extended keyboard (internal use only)
  VK_OEM_102 	    = 0xE2,   --  < or | on IBM-compatible 102 enhanced non-U.S. keyboard
  VK_ICO_HELP 	  = 0xE3,   --  Help on Olivetti extended keyboard (internal use only)
  VK_ICO_00 	    = 0xE4,   --  00 on Olivetti extended keyboard (internal use only)
  VK_ICO_CLEAR 	  = 0xE6,   --  Clear on Olivette extended keyboard (internal use only)
  VK_OEM_RESET 	  = 0xE9,   --  Reset (Nokia keyboards only)
  VK_OEM_JUMP 	  = 0xEA,   --  Jump (Nokia keyboards only)
  VK_OEM_PA1 	    = 0xEB,   --  PA1 (Nokia keyboards only)
  VK_OEM_PA2 	    = 0xEC,   --  PA2 (Nokia keyboards only)
  VK_OEM_PA3 	    = 0xED,   --  PA3 (Nokia keyboards only)
  VK_OEM_WSCTRL 	= 0xEE,   --  WSCTRL (Nokia keyboards only)
  VK_OEM_CUSEL 	  = 0xEF,   --  CUSEL (Nokia keyboards only)
  VK_OEM_ATTN 	  = 0xF0,   --  ATTN (Nokia keyboards only)
  VK_OEM_FINNISH 	= 0xF1,   --  FINNISH (Nokia keyboards only)
  VK_OEM_COPY 	  = 0xF2,   --  COPY (Nokia keyboards only)
  VK_OEM_AUTO 	  = 0xF3,   --  AUTO (Nokia keyboards only)
  VK_OEM_ENLW 	  = 0xF4,   --  ENLW (Nokia keyboards only)
  VK_OEM_BACKTAB 	= 0xF5,   --  BACKTAB (Nokia keyboards only)
  VK_ATTN 	      = 0xF6,   --  ATTN
  VK_CRSEL 	      = 0xF7,   --  CRSEL
  VK_EXSEL 	      = 0xF8,   --  EXSEL
  VK_EREOF 	      = 0xF9,   --  EREOF
  VK_PLAY 	      = 0xFA,   --  PLAY
  VK_ZOOM 	      = 0xFB,   --  ZOOM
  VK_NONAME 	    = 0xFC,   --  NONAME
  VK_PA1 	        = 0xFD,   --  PA1
  VK_OEM_CLEAR 	  = 0xFE,   --  CLEAR
}


KEYCODES_NAME = {
  "VK_LBUTTON", 	
  "VK_RBUTTON", 	
  "VK_CANCEL", 	
  "VK_MBUTTON", 
  "",
  "",
  "",
  "VK_BACK", 
  "VK_TAB", 	
  "",
  "",
  "VK_CLEAR", 	
  "VK_ENTER", 	
  "",
  "",
  "VK_SHIFT", 
  "VK_CONTROL", 	
  "VK_MENU", 	
  "VK_PAUSE", 	
  "VK_CAPITAL", 	
  "",
  "",
  "",
  "",
  "",
  "",
  "VK_ESCAPE", 	
  "",
  "",
  "",
  "",
  "VK_SPACE", 
  "VK_PAGEUP", 	
  "VK_PAGEDOWN", 
  "VK_END", 	
  "VK_HOME", 	
  "VK_LEFT", 	
  "VK_UP", 	
  "VK_RIGHT", 
  "VK_DOWN", 
  "VK_SELECT", 	
  "VK_PRINT", 	
  "VK_EXECUTE", 	
  "VK_SNAPSHOT", 	
  "VK_INSERT", 
  "VK_DELETE", 	
  "VK_HELP", 
  "VK_0", 
  "VK_1", 
  "VK_2", 
  "VK_3", 
  "VK_4", 
  "VK_5", 
  "VK_6", 
  "VK_7", 
  "VK_8", 
  "VK_9", 
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "VK_A", 
  "VK_B", 
  "VK_C", 
  "VK_D", 
  "VK_E", 
  "VK_F", 
  "VK_G", 
  "VK_H", 
  "VK_I", 
  "VK_J", 
  "VK_K", 
  "VK_L", 
  "VK_M", 
  "VK_N", 
  "VK_O", 
  "VK_P", 
  "VK_Q", 
  "VK_R", 
  "VK_S", 
  "VK_T", 
  "VK_U", 
  "VK_V", 
  "VK_W", 
  "VK_X", 
  "VK_Y", 
  "VK_Z", 
  "VK_STARTKEY", 	
  "",
  "VK_CONTEXTKEY", 	
  "",
  "",
  "VK_NUMPAD0", 	
  "VK_NUMPAD1", 	
  "VK_NUMPAD2", 	
  "VK_NUMPAD3", 	
  "VK_NUMPAD4", 	
  "VK_NUMPAD5", 	
  "VK_NUMPAD6", 	
  "VK_NUMPAD7", 	
  "VK_NUMPAD8", 	
  "VK_NUMPAD9", 	
  "VK_MULTIPLY", 	
  "VK_ADD", 	
  "VK_SEPARATOR", 
  "VK_SUBTRACT", 	
  "VK_DECIMAL", 	
  "VK_DIVIDE", 	
  "VK_F1", 	
  "VK_F2", 	
  "VK_F3", 	
  "VK_F4", 	
  "VK_F5", 	
  "VK_F6", 	
  "VK_F7", 	
  "VK_F8", 	
  "VK_F9", 	
  "VK_F10", 	
  "VK_F11", 	
  "VK_F12", 	
  "VK_F13", 	
  "VK_F14", 	
  "VK_F15", 	
  "VK_F16", 	
  "VK_F17", 	
  "VK_F18", 	
  "VK_F19", 	
  "VK_F20", 	
  "VK_F21", 	
  "VK_F22", 	
  "VK_F23", 	
  "VK_F24", 	
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "VK_NUMLOCK", 	
  "VK_OEM_SCROLL", 
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "VK_OEM_1", 	
  "VK_OEM_PLUS", 	
  "VK_OEM_COMMA", 	
  "VK_OEM_MINUS", 
  "VK_OEM_PERIOD", 
  "VK_OEM_2", 	
  "VK_OEM_3", 
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "",
  "VK_OEM_4", 	
  "VK_OEM_5", 	
  "VK_OEM_6", 	
  "VK_OEM_7", 	
  "VK_OEM_8", 
  "VK_ICO_F17",
  "VK_ICO_F18", 	
  "VK_OEM_102", 
  "VK_ICO_HELP", 
  "VK_ICO_00", 	
  "",
  "VK_ICO_CLEAR", 	
  "",
  "",
  "VK_OEM_RESET", 	
  "VK_OEM_JUMP", 	
  "VK_OEM_PA1", 
  "VK_OEM_PA2", 	
  "VK_OEM_PA3", 	
  "VK_OEM_WSCTRL", 	
  "VK_OEM_CUSEL", 	
  "VK_OEM_ATTN", 	
  "VK_OEM_FINNISH", 	
  "VK_OEM_COPY", 	
  "VK_OEM_AUTO", 	
  "VK_OEM_ENLW", 	
  "VK_OEM_BACKTAB", 
  "VK_ATTN", 
  "VK_CRSEL", 	
  "VK_EXSEL", 
  "VK_EREOF", 
  "VK_PLAY", 
  "VK_ZOOM", 
  "VK_NONAME",
  "VK_PA1",
  "VK_OEM_CLEAR",
  "",
}

----------------------------------------------------------------------------------------------------------
function msg(param, clr) 
  if clr then reaper.ClearConsole() end 
  reaper.ShowConsoleMsg(tostring(param).."\n") 
end

----------------------------------------------------------------------------------------------------------
function main()
  time_now = reaper.time_precise()

  -- keys_state = reaper.JS_VKeys_GetState(time_keys_prev):sub(key_low, key_hi)
  keys_down = reaper.JS_VKeys_GetDown(time_keys_prev):sub(key_low, key_hi)
  do_quit = false

  if keys_down ~= keys_prev and keys_down ~= keys_zero then
    local p = 0

    repeat
      p = keys_down:find("\1", p+1)

      if p then 
        if keys_prev:byte(p) == 0 then 
          asc_code = key_low + p - 1
          if asc_code == 27 then do_quit = true end
          msg(string.format("key=\'%c\' %03d $%02x %s", asc_code,  asc_code, asc_code, KEYCODES_NAME[asc_code]))
        end
      end
    until p == nil
  end

  time_keys_prev = time_now
  -- keys_prev = keys_state  
  keys_prev = keys_down  

  gfx.x, gfx.y = 10, 10
  gfx.drawstr("Press keys, then copy console")
  gfx.x, gfx.y = 10, 30
  gfx.drawstr("Press Esc when done!")
  gfx.update()

  if not(do_quit or gfx.getchar() == -1) then
    reaper.defer(main)
  end
end

-----------------------------------------------------------------------------------------
function quit()
  gfx.quit()
  reaper.JS_VKeys_Intercept(-1, -1)
end

-----------------------------------------------------------------------------------------

msg("",true)

gfx.init("key tests", 300, 100, 0, 50,50)


reaper.atexit(quit)

reaper.JS_VKeys_Intercept(-1, 1)

main()
__________________
Reaper's lunatic
Reapack repository / GitHub / SoundCloud / Donate

Last edited by binbinhfr; 10-27-2022 at 04:10 AM.
binbinhfr is offline   Reply With Quote
Old 10-31-2022, 12:40 AM   #13
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

There is AutoHotkey that might do what you need.
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Old 12-09-2023, 11:18 AM   #14
saxmand
Human being with feelings
 
saxmand's Avatar
 
Join Date: Dec 2023
Location: Denmark
Posts: 21
Default

This works for me

Took a few hours to make it work, so through I'd share. It doesn't run any code in the background.

EDIT: been using if for some days and made it work even better:

Code:
local function main(keyPress)
-- Your function that you can filter by the different modifiers and keystrokes you'll attach to the command
end

local keysObj = {
    _13 = 'enter',
    _27 = 'escape',

    _16 = 'shift',
    _17 = 'cmd',
    _18 = 'alt',
    _91 = 'ctrl',

     _37 = 'left',
     _38 = 'up',
     _39 = 'right',
    _40 = 'down',

    _43 = '+',

    _49 = 1,
    _50 = 2,
    _51 = 3,
    _52 = 4,
    _53 = 5,
    _54 = 6,
    _55 = 7,
    _56 = 8,
    _57 = 9,
    _48 = 0,

    _65 = 'A',
    _66 = 'B',
    _67 = 'C',
    _68 = 'D',
    _69 = 'E',
    _70 = 'F',
    _71 = 'G',
    _72 = 'H',
    _73 = 'I',
    _74 = 'J',
    _75 = 'K',
    _76 = 'L',
    _77 = 'M',
    _78 = 'N',
    _79 = 'O',
    _80 = 'P',
    _81 = 'Q',
    _82 = 'R',
    _83 = 'S',
    _84 = 'T',
    _85 = 'U',
    _86 = 'V',
    _87 = 'W',
    _88 = 'X',
    _89 = 'Y',
    _90 = 'Z',

    _230 = 'æ',
    _248 = 'ø',
    _229 = 'å',
    _198 = 'Æ',
    _216 = 'Ø',
    _197 = 'Å',

     _44 = ',',
     _46 = '.',
     _59 = ';',
     _58 = ':',

    _96 = 0,
    _97 = 1,
    _98 = 2,
    _99 = 3,
    _100 = 4,
    _101 = 5,
    _102 = 6,
    _103 = 7,
    _104 = 8,
    _105 = 9,
    _106 = '*',
    _107 = '+',
    _108 = '=',
    _109 = '-',
    _110 = ',',
    _111 = '/',

    _144 = 'clear'
}
cmd = false
shift = false
alt = false
ctrl = false

if reaper.APIExists("JS_VKeys_Intercept") then
  for i = 0, 255 do -- What key is down?
    if reaper.JS_VKeys_GetState(0):byte(i) == 1 then
      local keyPress = keysObj['_' .. i]
      if keyPress == nil then
        reaper.ShowConsoleMsg('Key not mapped: ' .. i .. '\n')
      else
        if keyPress == 'alt' then
          alt = true
        elseif keyPress == 'shift'then
          shift = true 
        elseif keyPress == 'cmd' then
          cmd = true
        elseif keyPress == 'ctrl' then 
          ctrl = true
        else 
          mainKey = keyPress
        end
      end
      reaper.JS_VKeys_Intercept(i, -1)
    end
  end
else
  reaper.MB("This script requires the js_ReaScriptAPI extension!", "Missing API", 0)
  reaper.defer(function () end)
end


if mainKey then
  main(mainKey)
end

Last edited by saxmand; 12-15-2023 at 09:09 AM.
saxmand is offline   Reply With Quote
Old 12-28-2023, 03:17 PM   #15
saxmand
Human being with feelings
 
saxmand's Avatar
 
Join Date: Dec 2023
Location: Denmark
Posts: 21
Default

For anyone that'll maybe find this topic later:

So playing around more with this I found that you can get it much easier using the get_action_context function. It needs a bit of string parsing. But with the code below it makes it very easy to have a script that will react differently to different key commands.

Below is what I have so far:

Code:
local function main(keyPress)
-- Your function that you can filter by the different modifiers and keystrokes you'll attach to the command
end

local is_new_value,filename,sectionID,cmdID,mode,resolution,val,contextstr = reaper.get_action_context()

local KEYCODES = {
    _1 = "The left mouse button",
    _2 = "The right mouse button",
    _3 = "The Cancel virtual key, used for control-break processing",
    _4 = "The middle mouse button",
    _8 = "Backspace",
    _9 = "Tab",
    _12 = "5 (keypad without Num Lock)",
    _13 = "Enter",
    _16 = "Shift (either one)",
    _17 = "Ctrl (either one)",
    _18 = "Alt (either one)",
    _19 = "Pause",
    _20 = "Caps Lock",
    _27 = "Esc",
    _32 = "Spacebar",
    _33 = "Page Up",
    _34 = "Page Down",
    _35 = "End",
    _36 = "Home",
    _37 = "Left Arrow",
    _38 = "Up Arrow",
    _32807 = "Right Arrow",
    _32808 = "Down Arrow",
    _32805 = "Left Arrow",
    _32806 = "Up Arrow",
    _39 = "Right Arrow",
    _40 = "Down Arrow",
    _41 = "Select",
    _42 = "Print (only used by Nokia keyboards)",
    _43 = "Execute (not used)",
    _44 = "Print Screen",
    _45 = "Insert",
    _46 = "Delete",
    _47 = "Help",
    _48 = 0,
    _49 = 1,
    _50 = 2,
    _51 = 3,
    _52 = 4,
    _53 = 5,
    _54 = 6,
    _55 = 7,
    _56 = 8,
    _57 = 9,
    _65 = "A",
    _66 = "B",
    _67 = "C",
    _68 = "D",
    _69 = "E",
    _70 = "F",
    _71 = "G",
    _72 = "H",
    _73 = "I",
    _74 = "J",
    _75 = "K",
    _76 = "L",
    _77 = "M",
    _78 = "N",
    _79 = "O",
    _80 = "P",
    _81 = "Q",
    _82 = "R",
    _83 = "S",
    _84 = "T",
    _85 = "U",
    _86 = "V",
    _87 = "W",
    _88 = "X",
    _89 = "Y",
    _90 = "Z",
    _91 = "Start Menu key",
    _93 = "Context Menu key",
    _96 = 0, --keypad
    _97 = 1, --keypad
    _98 = 2, --keypad
    _99 = 3, --keypad
    _100 = 4, --keypad
    _101 = 5, --keypad
    _102 = 6, --keypad
    _103 = 7, --keypad
    _104 = 8, --keypad
    _105 = 9, --keypad
    _106 = "keypad *", --keypad
    _107 = "keypad +", --keypad
    _108 = "keypad =",
    _109 = "keypad -", --keypad
    _110 = "keypad .", --keypad
    _111 = "keypad /", --keypad
    _112 = "F1",
    _113 = "F2",
    _114 = "F3",
    _115 = "F4",
    _116 = "F5",
    _117 = "F6",
    _118 = "F7",
    _119 = "F8",
    _120 = "F9",
    _121 = "F10",
    _122 = "F11",
    _123 = "F12",
    _124 = "F13",
    _125 = "F14",
    _126 = "F15",
    _127 = "F16",
    _128 = "F17",
    _129 = "F18",
    _130 = "F19",
    _131 = "F20",
    _132 = "F21",
    _133 = "F22",
    _134 = "F23",
    _135 = "F24",
    _144 = "Num Lock",
    _145 = "Scroll Lock",
    _186 = ";",
    _187 = "=",
    _188 = ",",
    _189 = "-",
    _190 = ".",
    _191 = "/",
    _192 = "`",
    _219 = "[",
    _220 = "\\",
    _221 = "]",
    _222 = "'",
    _223 = "(unknown)"
}

--contextstr = "key:VSW:100"
isKey = string.sub(contextstr,1,3) == 'key'
inputStr =  string.sub(contextstr,5) or ""
positionOfSplit = string.find(inputStr, ":") or 0
keys =  string.sub(inputStr,1,positionOfSplit -1) 
number = string.sub(inputStr,positionOfSplit+1)


alt = string.match(keys, "A") == "A" 
shift = string.match(keys, "S") == "S" 
cmd = string.match(keys, "C") == "C" 
ctrl = string.match(keys, "W") == "W" 
mainKey = KEYCODES["_" .. number] or ""           

--[[
reaper.ShowConsoleMsg(
"isKey: " .. tostring(isKey).."\n" ..
"inputStr: "  .. inputStr .. "\n" ..
"keys: "  .. keys .. "\n" ..
"alt: "  .. tostring(alt) .. "\n" ..
"shift: "  .. tostring(shift) .. "\n" ..
"cmd: "  .. tostring(cmd) .. "\n" ..
"ctrl: "  .. tostring(ctrl) .. "\n" ..
"mainKey: "  .. mainKey .. "\n" 
)]]--
          
                
if mainKey ~= "" then main(mainKey) end
__________________
Reaper is my new spirit animal!
saxmand is offline   Reply With Quote
Old 12-30-2023, 05:09 AM   #16
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Quote:
Originally Posted by saxmand View Post
For anyone that'll maybe find this topic later:

So playing around more with this I found that you can get it much easier using the get_action_context function. It needs a bit of string parsing. But with the code below it makes it very easy to have a script that will react differently to different key commands.

Below is what I have so far:

Code:
local function main(keyPress)
-- Your function that you can filter by the different modifiers and keystrokes you'll attach to the command
end

local is_new_value,filename,sectionID,cmdID,mode,resolution,val,contextstr = reaper.get_action_context()

local KEYCODES = {
    _1 = "The left mouse button",
    _2 = "The right mouse button",
    _3 = "The Cancel virtual key, used for control-break processing",
    _4 = "The middle mouse button",
    _8 = "Backspace",
    _9 = "Tab",
    _12 = "5 (keypad without Num Lock)",
    _13 = "Enter",
    _16 = "Shift (either one)",
    _17 = "Ctrl (either one)",
    _18 = "Alt (either one)",
    _19 = "Pause",
    _20 = "Caps Lock",
    _27 = "Esc",
    _32 = "Spacebar",
    _33 = "Page Up",
    _34 = "Page Down",
    _35 = "End",
    _36 = "Home",
    _37 = "Left Arrow",
    _38 = "Up Arrow",
    _32807 = "Right Arrow",
    _32808 = "Down Arrow",
    _32805 = "Left Arrow",
    _32806 = "Up Arrow",
    _39 = "Right Arrow",
    _40 = "Down Arrow",
    _41 = "Select",
    _42 = "Print (only used by Nokia keyboards)",
    _43 = "Execute (not used)",
    _44 = "Print Screen",
    _45 = "Insert",
    _46 = "Delete",
    _47 = "Help",
    _48 = 0,
    _49 = 1,
    _50 = 2,
    _51 = 3,
    _52 = 4,
    _53 = 5,
    _54 = 6,
    _55 = 7,
    _56 = 8,
    _57 = 9,
    _65 = "A",
    _66 = "B",
    _67 = "C",
    _68 = "D",
    _69 = "E",
    _70 = "F",
    _71 = "G",
    _72 = "H",
    _73 = "I",
    _74 = "J",
    _75 = "K",
    _76 = "L",
    _77 = "M",
    _78 = "N",
    _79 = "O",
    _80 = "P",
    _81 = "Q",
    _82 = "R",
    _83 = "S",
    _84 = "T",
    _85 = "U",
    _86 = "V",
    _87 = "W",
    _88 = "X",
    _89 = "Y",
    _90 = "Z",
    _91 = "Start Menu key",
    _93 = "Context Menu key",
    _96 = 0, --keypad
    _97 = 1, --keypad
    _98 = 2, --keypad
    _99 = 3, --keypad
    _100 = 4, --keypad
    _101 = 5, --keypad
    _102 = 6, --keypad
    _103 = 7, --keypad
    _104 = 8, --keypad
    _105 = 9, --keypad
    _106 = "keypad *", --keypad
    _107 = "keypad +", --keypad
    _108 = "keypad =",
    _109 = "keypad -", --keypad
    _110 = "keypad .", --keypad
    _111 = "keypad /", --keypad
    _112 = "F1",
    _113 = "F2",
    _114 = "F3",
    _115 = "F4",
    _116 = "F5",
    _117 = "F6",
    _118 = "F7",
    _119 = "F8",
    _120 = "F9",
    _121 = "F10",
    _122 = "F11",
    _123 = "F12",
    _124 = "F13",
    _125 = "F14",
    _126 = "F15",
    _127 = "F16",
    _128 = "F17",
    _129 = "F18",
    _130 = "F19",
    _131 = "F20",
    _132 = "F21",
    _133 = "F22",
    _134 = "F23",
    _135 = "F24",
    _144 = "Num Lock",
    _145 = "Scroll Lock",
    _186 = ";",
    _187 = "=",
    _188 = ",",
    _189 = "-",
    _190 = ".",
    _191 = "/",
    _192 = "`",
    _219 = "[",
    _220 = "\\",
    _221 = "]",
    _222 = "'",
    _223 = "(unknown)"
}

--contextstr = "key:VSW:100"
isKey = string.sub(contextstr,1,3) == 'key'
inputStr =  string.sub(contextstr,5) or ""
positionOfSplit = string.find(inputStr, ":") or 0
keys =  string.sub(inputStr,1,positionOfSplit -1) 
number = string.sub(inputStr,positionOfSplit+1)


alt = string.match(keys, "A") == "A" 
shift = string.match(keys, "S") == "S" 
cmd = string.match(keys, "C") == "C" 
ctrl = string.match(keys, "W") == "W" 
mainKey = KEYCODES["_" .. number] or ""           

--[[
reaper.ShowConsoleMsg(
"isKey: " .. tostring(isKey).."\n" ..
"inputStr: "  .. inputStr .. "\n" ..
"keys: "  .. keys .. "\n" ..
"alt: "  .. tostring(alt) .. "\n" ..
"shift: "  .. tostring(shift) .. "\n" ..
"cmd: "  .. tostring(cmd) .. "\n" ..
"ctrl: "  .. tostring(ctrl) .. "\n" ..
"mainKey: "  .. mainKey .. "\n" 
)]]--
          
                
if mainKey ~= "" then main(mainKey) end
Important note: this detects the shortcut with which the action was started, so it's limited to the shortcuts you associated with the action.
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 01-04-2024, 04:32 PM   #17
saxmand
Human being with feelings
 
saxmand's Avatar
 
Join Date: Dec 2023
Location: Denmark
Posts: 21
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
Important note: this detects the shortcut with which the action was started, so it's limited to the shortcuts you associated with the action.
Ah yes, I guess I while working on it have missread OPs original post as I was looking for something to actually just know what shortcut is being pressed, also since the title of the post could be interpreted that way.

Thanks for catching this!
__________________
Reaper is my new spirit animal!
saxmand is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.