Questions about Keymapping and User I/O in Celx

All about writing scripts for Celestia in Lua and the .cel system
Avatar
Topic author
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Questions about Keymapping and User I/O in Celx

Post #1by Chuft-Captain » 17.01.2007, 10:57

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.
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #2by selden » 17.01.2007, 11:31

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.
Selden

Christophe
Developer
Posts: 944
Joined: 18.07.2002
With us: 22 years 4 months
Location: Lyon (France)

Post #3by Christophe » 17.01.2007, 22:32

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
Christophe

Avatar
Topic author
Chuft-Captain
Posts: 1779
Joined: 18.12.2005
With us: 18 years 11 months

Post #4by Chuft-Captain » 18.01.2007, 13:16

Thanks very much, that's helped a lot. :)
"Is a planetary surface the right place for an expanding technological civilization?"
-- Gerard K. O'Neill (1969)

CATALOG SYNTAX HIGHLIGHTING TOOLS LAGRANGE POINTS


Return to “Scripting”