Accelerator for Right+Numpad?

Posted by Kahenraz on Fri 26 Aug 2016 04:11 PM — 7 posts, 29,323 views.

#0
I'd like to add an accelerator for a combination of the right arrow and numpad directional keys for fleeing from combat so that I can run around fighting stuff with just one hand without having to bring a second one in just to flee.

I tried the following but it did not work:

AcceleratorTo ("Right+Numpad8", "flee north", sendto.world)


I assume the prefix only supports Ctrl/Alt/Shift.
USA Global Moderator #1
Quote:
I assume the prefix only supports Ctrl/Alt/Shift.

This is a limitation in the operating system. You can't just combine any two keys arbitrarily.

https://en.wikipedia.org/wiki/Modifier_key
#2
It's a limitation of the implementation not the operating system.

For example, this can be done with GetKeyState(), GetAsyncKeyState(), or GetKeyboardState().

It sounds like the current implementation uses RegisterHotKey(), which is very limited.
Australia Forum Administrator #3
The function RegisterHotKey is not anywhere in the source.

You can view that at http://www.gammon.com.au//git
Australia Forum Administrator #4
You could achieve this with a combination of

GetInfo (294) and AcceleratorTo:

For example:


AcceleratorTo ("Ctrl+Numpad8" , ' keyPressed ("Numpad8") ', sendto.script)


Now that calls the function keyPressed with the argument "Numpad8" when that key is pressed. The function call to keyPressed has to be quoted as we want it deferred (to when the key is pressed) and not evaluated right now when AcceleratorTo is called.

The function keyPressed could look like this:


function keyPressed (which)
  local keyState = GetInfo (294)
  if which == 'Numpad8' and bit.band (0x080, keyState) ~= 0 then
    print ("Numpad8 and right-control pressed")
  elseif which == 'Numpad8' and bit.band (0x010, keyState) ~= 0 then
    print ("Numpad8 and left-control pressed")
  else
    print ("Some other key pressed")
  end -- if
  
end -- keyPressed


Notice that we are testing the key state inside the function.

However I wasn't getting that to work reliably, possibly because I test inside a virtual machine. You may be able to get some combination of those ideas to work.


Template:function=GetInfo
GetInfo

The documentation for the GetInfo script function is available online. It is also in the MUSHclient help file.



Template:function=AcceleratorTo
AcceleratorTo

The documentation for the AcceleratorTo script function is available online. It is also in the MUSHclient help file.

Amended on Sun 28 Aug 2016 04:02 AM by Nick Gammon
Australia Forum Administrator #5
It looks like the documentation for the bit patterns in the help file is wrong (for GetInfo (294)). Below are the correct bits:


294 - Status of keyboard modifiers / mouse click, as follows:

      (values are in hexadecimal)

      0x00001 - shift key down (left or right)    
      0x00002 - control key down (left or right) 
      0x00004 - Alt key down (left or right)

      0x00008 - left shift key down     
      0x00010 - right shift key down     

      0x00020 - left control key down  
      0x00040 - right control key down

      0x00080 - left Alt key down  
      0x00100 - right Alt key down

      0x00200 - Caps Lock key down (right now)    
      0x00400 - Num Lock key down (right now)
      0x00800 - Scroll Lock key down (right now)

      0x02000 - Caps Lock key toggled (active)  
      0x04000 - Num Lock key  toggled (active)
      0x08000 - Scroll Lock key  toggled (active)

      0x10000 - Left mouse button down  
      0x20000 - Right mouse button down
      0x40000 - Middle mouse button down


I got it to work OK with the Alt key like this:


function keyPressed (which)
  local keyState = GetInfo (294)
  if which == 'Numpad8' and bit.band (0x080, keyState) ~= 0 then
    print ("Numpad8 and left-Alt pressed")
  elseif which == 'Numpad8' and bit.band (0x0100, keyState) ~= 0 then
    print ("Numpad8 and right-Alt pressed")
  else
    print ("Some other key pressed")
  end -- if
end -- keyPressed


With this to add the accelerator:


AcceleratorTo ("Alt+Numpad8" , ' keyPressed ("Numpad8") ', sendto.script)


I've uploaded the corrected documentation to this server, so it is OK now if you follow the link:

http://www.gammon.com.au/scripts/doc.php?function=GetInfo
Amended on Sun 28 Aug 2016 03:58 AM by Nick Gammon
Australia Forum Administrator #6
After re-reading your question I see you mentioned right-arrow, not right-ctrl, which is a bit different.

I don't think you can easily do that. The action of pressing right-arrow has an immediate effect - the cursor moves to the right in the command window.

I don't think you can reasonably defer that - just in case you press numpad-8 a moment later, that would annoy the hell out of a lot of users.

I think, though, that my suggestion of detecting right-control, right-alt or right-shift could still work for you. Those keys are fairly close to the numpad keys, so you could press them together without too much trouble.