input-pad
Posted: Thu Jul 27, 2017 8:48 pm
Hi everybody...
needed an input-pad. Thought, it could be helpful for you as well...
the keyboard-layout can be easily adapted. see DATA...
the functions don't depend on any settings such as option base.
bye, joel.
needed an input-pad. Thought, it could be helpful for you as well...
the keyboard-layout can be easily adapted. see DATA...
the functions don't depend on any settings such as option base.
bye, joel.
Code: Select all
/*
COLOURS used in code
'c'-description
'y'-testing
'b'-defining
''
*/
'c'============================
'*******************
'*** inputfield ***
'*******************
'owns all variables
'creates array
'b'============================
DEF inputfield
'variables
key_width=100 ! key_height=100
x_keygap=key_width*.1 ! y_keygap=key_height*.1 'gap between keys
IF NOT called THEN 'if called for the first time
DIM key$(20,20) 'depends on number of keys in row, or column
DIM keyboard_width(20) 'depends on number of keys in a row
keyboard_height=0 'resetting keyboard_height. beeing filled later...
key_y=0 'counting keyboard->row
'setting up key$(column, row)
WHILE DATA_EXIST()
key_y+=1 'next key in row
READ key_data1$ 'reads keyboardlayout...row by row
keyboard_width(key_y)=LEN(key_data1$) 'calculating number of keys by LEN of string to be read
string_to_2d_array_x(key$,1,key_y,key_data1$) 'fills string in key$ over running xstarting by 1, y=key_y
END WHILE
keyboard_height=key_y
END IF 'called?
called=1
'defining keyboard layout
'DATA "ABC","DEF","123+","456-","789*",".0,/"
'DATA "QWERTZUIOP","ASDFGHJKLÖÄ"
DATA "123+","456-","789*","*0#/"
END DEF
'c'============================
'********************************
'*** inputfield_create(x1,y1) ***
'********************************
'creates keyboard graphic
'x1, x2: Position of upper left corner
'b'============================
DEF inputfield_create(x1,y1)
IF NOT called THEN
inputfield.x1=x1 ! inputfield.y1=y1
keyboard_height=inputfield.keyboard_height 'keyboard_width()=inputfield.keyboard_width() doesnt work - array!!
key_width=inputfield.key_width ! key_height=inputfield.key_height
x_keygap=inputfield.x_keygap ! y_keygap=inputfield.y_keygap
ENDIF 'not called
'building up key-graphic
FOR key_y=1 TO keyboard_height
FOR key_x=1 TO inputfield.keyboard_width(key_y)
xl=gridcoord(key_x-1,key_width,x1)+x_keygap 'vertical left line
xr=gridcoord(key_x,key_width,x1)-x_keygap 'vertical right line
yu=gridcoord(key_y-1,key_height,y1)+y_keygap 'horizontal upper line
yd=gridcoord(key_y,key_height,y1)-y_keygap 'horizontal lower line
xc=gridcoord(key_x-1,key_width,x1)+key_width/2 'center_x
yc=gridcoord(key_y-1,key_height,y1)+key_height/2 'center_y
DRAW RECT xl,yu TO xr,yd 'draws outer boundry
DRAW TEXT inputfield.key$(key_x,key_y) AT xc,yc
NEXT key_x
NEXT key_y
called=1
END DEF
'c'============================
'*********************************
'*** inputfield_identify$(x,y) ***
'*********************************
'identifies pressed key
'x,y: coordinate of a-system
'b'============================
DEF inputfield_identify$(x,y)
IF NOT called THEN
key_width=inputfield.key_width ! key_height=inputfield.key_height
x1=inputfield.x1 ! y1=inputfield.y1
ENDIF 'not called
key_x=gridpos_f(x,key_width,x1)+1
key_y=gridpos_f(y,key_height,y1)+1
RETURN inputfield.key$(key_x,key_y)
called = 1
END DEF
'c'============================
'********************************
'*** gridcoord(n,grid,anchor) ***
'********************************
'returns coordinates of n where a grid is defined with
'
'n: given in grid-units
'grid: distance between two gridlines
'anchor: coordinates of the 0-position of the grid
'x,y: coordinate of a-system
'b'============================
DEF gridcoord(n,grid,anchor)
n=INT(n)
gridcoord=anchor+grid*n
END DEF
'c'============================
'********************************
'*** gridpos_f(a,grid,anchor) ***
'********************************
'returns 'floor-rounded' position of a point in 'gridunits'
'where grid is the distance between two gridlines given in normal coordinates
'anchor is the 0-position of the grid in normal coordinates
'b'============================
DEF gridpos_f(a,grid,anchor)
RETURN FLOOR((a-anchor)/grid)
END DEF
'c'============================
'*********************************************************************
' ****** string_to_2d_array_x(array$(,,),x_start,dim_y,string$) ******
'*********************************************************************
'imports a string letterwise into an array, where x-Dimension is variable and y,z is steady
'Syntax: (array$(,,): array, where string$ is imported, x_start: index of x where first letter is imported
'dim_y, dim_z: steady index, string$: string to be imported
'b'============================
DEF string_to_2d_array_x(array$(,),x_start,dim_y,string$)
FOR n= x_start TO LEN(string$)
array$(n,dim_y)=MID$(string$,n-1+OPTION_BASE(),1)
NEXT n
END DEF
'y'
'****** testprogram ******* this way round because of SB-BUG
OPTION TEXT POS CENTRAL
'OPTION BASE 1 'functions don't depend on option_base()
FIELD "output" TEXT "" AT SCREEN_WIDTH()*0.1,SCREEN_HEIGHT()*.9 SIZE SCREEN_WIDTH()*.8,SCREEN_HEIGHT()*.1 RO 'output field
inputfield 'setup keyboard-layout and array
GRAPHICS
GRAPHICS CLEAR
inputfield_create(SCREEN_WIDTH()*.3,0) 'create graphics at given coordinates
main_loop1:
SLOWDOWN
GET TOUCH 0 AS x,y
IF x=-1 THEN GOTO main_loop1
main_loop2:
GET TOUCH 0 AS x1,y1
IF x1>-1 THEN main_loop2 'touch released?
keypressed$=keypressed$&inputfield_identify$(x,y) 'get key
FIELD "output" TEXT keypressed$ 'show pressed key
GOTO main_loop1