Page 1 of 1

Questions about Keymapping and User I/O in Celx

Posted: 17.01.2007, 10:57
by Chuft-Captain
A couple of questions about Hank's code:
(from this post: http://celestiaproject.net/forum/viewtopic.php ... ght=#76103)
hank wrote:

Code: Select all

dolly =
   function(dollyFactor)
      obs = celestia:getobserver();
      obsPos = obs:getposition();
      sel = celestia:getselection();
      if sel == nil or sel:name() == "?" then return end
      selPos = sel:getposition();
      relPos = selPos:vectorto(obsPos);
      newPos = selPos:addvector( relPos * dollyFactor );
      obs:setposition(newPos);
   end

dollyFactor = 0.9;

dollyin =
   function()
     celestia:flash("Dolly In", 0.5);
     dolly( dollyFactor );
   end
   
dollyout =
   function()
     celestia:flash("Dolly Out", 0.5);
     dolly( 1/dollyFactor );
   end
      
keymap =
   {
       i = dollyin;
       o = dollyout;
   }

celestia_keyboard_callback = function(key)
  f = keymap[key]
   if f then
     f()
      return true;
   end
   return false;
   end



celestia:requestkeyboard(true);

celestia:flash("dolly test", 2);
wait(2);
repeat
   wait(0);
until false;


- Hank

1. Does anyone know how to map functions to keys which are not alphabetical?
eg. instead of "i" and "o" keys, what if I wanted to map to the numeric keys [1, 2, ..9, 0] at the top of the keypad? .. or perhaps alt-1, alt-2, etc. (or maybe even the function keys F1 - F12 ! )
You cannot just replace:

Code: Select all

keymap =
   {
       i = dollyin;
       o = dollyout;
   }

with:

Code: Select all

keymap =
   {
       1 = dollyin;
       2 = dollyout;
   }

This doesn't work, So perhaps we need to map to an ascii value for special characters, but what's the syntax?

2. Is it possible to prompt the user to set the value of a variable such as dollyfactor, instead of hardcoding it?
eg.

Code: Select all

dollyFactor = tonumber(getLine("Enter Dollyfactor:"))

This also, does NOT work.

Posted: 17.01.2007, 11:31
by selden
Different examples of how to get keyboard input are shown on Harald Schmidt's website at http://celestia.h-schmidt.net/

See his "Stopclock" function, for example.

Posted: 17.01.2007, 22:32
by Christophe
About question 1. Celx uses ASCII codes only for keyboard input, there is no other way to specify the use of a modifier key (SHIFT, CTRL, ALT, META) or a function key. However, the ASCII table does include CTRL-Key combinations in the 0x0 - 0x1F range.

Using 1 or 2 can work, but not with the way Hank does it, you can't use numbers as variable names and in this block:

Code: Select all

keymap =
   {
       i = dollyin;
       o = dollyout;
   }


i and o are lua variables. So if you want to use numeric keys you can't use the keymap 'hack' but you can do something like this:

Code: Select all

celestia_keyboard_callback = function(key)
   if key == '1' then
     dollyin()
      return true;
   end
   if key == '2' then
     dollyout()
      return true;
   end
   return false;
   end

Posted: 18.01.2007, 13:16
by Chuft-Captain
Thanks very much, that's helped a lot. :)