Post Reply 
my 1st program "Virtual Keyboard" [POC]
10-02-2015, 11:31 AM (This post was last modified: 10-06-2015 03:38 PM by primer.)
Post: #1
my 1st program "Virtual Keyboard" [POC]
Hello,
Here is my first program, a virtual keyboard for the prime, because changing orange key can't be performed by a firmware update Tongue
Well, don't be too enthusiastic, it's a first version, I had 2 free hours, it's only a "Proof of concept", it's ugly and not yet feature complete.

But at least, here are working features :
* moving cursor with left/right keys
* accept entry with ENTER key (or tap on right side)
* cancel entry by ESC key
* adding char at the end of the or in the middle (depending on cursor location)
* removing char at the end or middle... with "<=" (del) key.
* space key to add a space
* blinking cursor Smile

[Image: x5cp69.png]

Run the program with vkb or in USER mode with alpha dot key (.)

As it's a very first version, you may encounter bugs, you are wellcome to report it.

Note
I had trouble to work with MOUSE(), there is probably a way to enhance what I did, if someone want to take a look at my getmouse() function to leave me advices. I'm not sure if I should better check for MOUSE(4)==1 for type or not...

There is a feature that is not developed yet is to switch upper/lowercase...
more feature to come, depending on your interests...

Code:

// vkb  : virtual keyboard, by prime
// version 0.8 - 1st Oct 2015
// assigned on alpha-dot key

#pragma mode( separator(.,;) integer(b32) )


// forward decls (for main only)
drawkb();
drawtxt(txt);
chkkey();
addchar(c);

// main vars
local t:=150; // top pos kb
local f; // end req
local txt:=""; // text
local curs:=0; // cursor pos

// main prog
EXPORT vkb()
BEGIN
 local c:=""; // char
 f:=0;
 txt:="";
 curs=0;
 drawkb();
 REPEAT
  drawtxt();
  c:=chkkey();
  if c≠"" then addchar(c); end;
 UNTIL f;

 IF f==1 THEN
  RETURN(txt);
 ELSE
  RETURN (""); // cancelled
 END;
END;


//--- keys
//decl
movecurs(d);
getmouse();
dropchar();

chkkey()
BEGIN
 local k,m;
 // keyboard chk
 k:=GETKEY();
 CASE
  IF k==7 THEN // <-
   movecurs(-1);
  END;
  IF k==8 THEN // ->
   movecurs(1);
  END;
  IF k==19 THEN // del
  dropchar();
  END;
  IF k==30 THEN // ENTER
   f:=1; // ends
  END;
  IF k==4 THEN // esc
   f:=2; // ends
  END;
  IF k==49 THEN // spc
   return (" ");
  END;
 END; // case
 if f THEN RETURN ""; END;
 //mouse chk
 m:=getmouse();
 if m>0 then return CHAR(m+64); end;
 RETURN "";
END;

LOCAL r:=0; // release key
// get "key" from mouse
// return 1-26 for A-Z
getmouse()
BEGIN
 local x:=MOUSE(0);
 if x==-1 then r:=1; return 0; end; // no mouse activity
 if x>270 then f:=1; r:=1; return 0; end; // tap ok
 x:=IP(x/30);
 IF r==0 THEN RETURN 0; END; // not released
 r:=0; // pressed
 local y:=MOUSE(1);
 if y<150 then return 0; end; // out of scope
 y:=IP((y-150)/30);
 RETURN 1+x+(y*9);
END;

// move cursor position
movecurs(d)
BEGIN
 local n:=curs+d;
 if n<0  then  n:=0;  end;
 if n> DIM(txt)  then  n:=DIM(txt);  end;
 curs:=n;
END;


//--- txt
// decl
txtwcurs(c); // add cusror to the txt
local alt:=0; // alternance
drawtxt()
BEGIN
  rect_p(8,100,312,120,#AAAAAAh,#FFFFFFh);// clear txt
 if alt==0 then
  textout_p(txtwcurs("  "),10,100);
  alt:=1;
else
  textout_p(txtwcurs(CHAR(#25C1h)),10,100);
  alt:=0;
end;
WAIT(0.13);
END;

txtwcurs(c) // add cusror/char to the txt
 BEGIN
 local a:="";
 local b:=MID(txt,1+curs);
 if curs>0 then a:=LEFT(txt,curs); end;
 if curs==DIM(txt) then b:=""; end;
 RETURN a+c+b;
END;

// add a char to the string
addchar(c)
BEGIN
 txt:=txtwcurs(c);
// curs:=curs+1;
 movecurs(1);
END;

dropchar()
BEGIN
 local a:="";
 local b:=MID(txt,1+curs);
 if curs>1 then a:=LEFT(txt,curs-1); end;
 if curs==DIM(txt) then b:=""; end;
 txt:=a+b;
 movecurs(-1);
END;

//--- kb
drawatpos(x,y,c); // decl

drawkb()
 begin
 local a;
 rect_p(0,t,320,240);
 for a from 0 TO 90 step 30 do
  line_p(0,t+a,320,t+a);
 end;
 for a from 0 TO 270 step 30 do
  line_p(a,t,a,240);
 end;
 
 for a from 0 TO 25 do
  local x:=a MOD 9;
  local y:= IP(a/9);
  drawatpos(x,y,CHAR(65+a)); //65 up 97 lo 
 end;
 
 drawatpos(7.9,2,"aA"); // switch up/lo
 drawatpos(9,0,CHAR(#2713h)); //OK

 //FREEZE;
end;

drawatpos(x,y,c)
 begin
  textout_p(c,G0,10+x*30,t+8+y*30);
end;

//--- key assignation : dot key on alpha
KEY KA_Dot
BEGIN
 RETURN vkb();
END;

KEY SKA_Dot
BEGIN
 RETURN vkb();
END;


.hpprgm  vkb.hpprgm (Size: 6.12 KB / Downloads: 24)
edit : new vesion : key must be released, #pragma added
Let me know your feeling.
Best Regards,


Attached File(s) Thumbnail(s)
   

primer
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
my 1st program "Virtual Keyboard" [POC] - primer - 10-02-2015 11:31 AM



User(s) browsing this thread: 1 Guest(s)