une version que j'ai concue pour un projet personel, j'ai enlevé tout le reste autour
ca fonctionne, les touches flechees droite et gauche, supr, backspace,home,end sont gérées avec un curseur personalisable.
Code : Tout sélectionner
Declare cli()
Declare display(disp)
Declare scankeys()
Declare dokey(a)
Declare.i cursor()
Declare RefreshKeyboard()
Declare .s returnchar(key)
Declare Keydown(key)
Declare keyup(key)
Declare keypushed(key)
Declare keyon(key)
Declare.s getwallpaperpath()
Declare definecursor(cursor$)
Declare Output(st$)
;
; +--------------------------------------+
; | code par case @ purebasic forums |
; | case[at]moonshade.org |
; +--------------------------------------+
; | |
; | procedures d'interrogation |
; | clavier |
; | |
; +--------------------------------------+
; | |
; | PROCEDURES ET DOCUMENTATION |
; | |
; +--------------------------------------+
; | RefreshKeyboard() |
; | ***************** |
; | Récupere l'etat du clavier |
; | equivalent d'examinekeyboard() |
; | |
; | returnchar(key) |
; | *************** |
; | renvoi le caractere ascii de la |
; | touche 'key' |
; | |
; | Keydown(key) |
; | ************ |
; | renvoi 1 si la touche est enfoncee |
; | |
; | Keyup(key) |
; | ********** |
; | renvoi 1 si la touche est relachée |
; | |
; | keypushed(key) |
; | ************** |
; | renvoi 1 si la touche est appuyée |
; | mais ne l'etait pas avant |
; | |
; | keyon(key) |
; | ********** |
; | renvoi 1 si la touche est allumée |
; | numlock par exemple |
; | |
; +--------------------------------------+
; +--------------------------------------+
; | CONSTANTES DEFINITION DES TOUCHES |
; +--------------------------------------+
#AltKey_windows =91
#AltKey_Shift =16
#AltKey_LeftShift =160
#AltKey_RightShift =161
#AltKey_Alt =17
#AltKey_Left_Alt =164
#AltKey_AltGr =165
#AltKey_Esc =27
#AltKey_tilde =222
#AltKey_F1 =112
#AltKey_F2 =113
#AltKey_F3 =114
#AltKey_F4 =115
#AltKey_F5 =116
#AltKey_F6 =117
#AltKey_F7 =118
#AltKey_F8 =119
#AltKey_F9 =120
#AltKey_F10 =121
#AltKey_F11 =122
#AltKey_F12 =123
#AltKey_0 =48
#AltKey_1 =49
#AltKey_2 =50
#AltKey_3 =51
#AltKey_4 =52
#AltKey_5 =53
#AltKey_6 =54
#AltKey_7 =55
#AltKey_8 =56
#AltKey_9 =57
#AltKey_parenthese =219
#AltKey_egal =187
#AltKey_backspace =8
#AltKey_windows =91
#AltKey_Tab =9
#AltKey_A =65
#AltKey_B =66
#AltKey_C =67
#AltKey_D =68
#AltKey_E =69
#AltKey_F =70
#AltKey_G =71
#AltKey_H =72
#AltKey_I =73
#AltKey_J =74
#AltKey_K =75
#AltKey_L =76
#AltKey_M =77
#AltKey_N =78
#AltKey_O =79
#AltKey_P =80
#AltKey_Q =81
#AltKey_R =82
#AltKey_S =83
#AltKey_T =84
#AltKey_U =85
#AltKey_V =86
#AltKey_W =87
#AltKey_X =88
#AltKey_Y =89
#AltKey_Z =90
#AltKey_circ =221
#AltKey_dollar =186
#AltKey_return =13
#AltKey_caps =20
#AltKey_percent =192
#AltKey_multiply =220
#AltKey_compare =226
#AltKey_space =32
#altKey_left =37
#altKey_up =38
#altKey_right =39
#altKey_down =40
#AltKey_del =46
#AltKey_pageup =33
#altKey_pagedown =34
#altKey_fin =35
#altKey_debut =36
#AltKey_inser =45
#AltKey_prtscr =44
#AltKey_pause =19
#AltKey_mute =173
#AltKey_voldwn =174
#AltKey_volup =175
#AltKey_numlock =144
#AltKey_0num =96
#AltKey_1num =97
#AltKey_2num =98
#AltKey_3num =99
#AltKey_4num =100
#AltKey_5num =101
#AltKey_6num =102
#AltKey_7num =103
#AltKey_8num =104
#AltKey_9num =105
#Altkey_minnum =109
#Altkey_addnum =107
#altKey_leftmouse =1
#altKey_rightmouse =2
#AltKey_Left_Ctrl =162
#altKey_right_Ctrl =163
#altKey_middlemouse =4 ; pas sur que la valeur soit la meme suivant les souris a tester
;+--------------------------------------+
;| BUFFERS CLAVIER |
;+--------------------------------------
Global *buffer=AllocateMemory(256)
Global *state=AllocateMemory(256)
InitSprite()
ExamineDesktops()
UsePNGImageDecoder()
UseJPEGImageDecoder()
Global rdw=DesktopWidth(0),rdh=DesktopHeight(0)
Global toplog=0
Global xd,yd,styd
Global fnt=LoadFont(#PB_Any,"terminal",20)
tempw=OpenWindow(#PB_Any,0,0,1,1,"")
tempc=CanvasGadget(#PB_Any,0,0,1,1)
;---- CLI win
StartDrawing(CanvasOutput(tempc))
DrawingFont(FontID(fnt))
Global fth=TextHeight(" ")
Global ftw=TextWidth(" ")
StopDrawing()
Global dw=ftw*80
Global dh=fth*24
CloseWindow(tempw)
Global cli=OpenWindow(#PB_Any,0,0,dw,dh,"CMD" )
Global can2=CanvasGadget(#PB_Any,0,0,dw,dh)
Enumeration
#main=0
#render
#browser
#fnt
#lnk
#adressbar
EndEnumeration
Structure cursor
lastblink.i ; elapsed milliseconds
blinkrate.i ; milliseconds blinking interval
List colors.i() ; list of colors cycle -1=no display
on.i ; color for on cursor
off.i ; color for off
state.i
pos.i; pos in string
scpos.i ; pos on screen
ch.i ; charcater used for cursor
col.i ;color for the cursor
;----annimated cursor
List char.i()
frame.i
lastframe.i
animrate.i
EndStructure
Structure key
id.i
time.i
Repeat.i
EndStructure
Global lastwin.s
Global Debg=0
Global lastkey.key
Global NewList logs.s()
Global wallpaper=LoadImage(#PB_Any,getwallpaperpath())
Global prompt$=">"
Global cli$=""
Global fc=$00ffff
Global bc=$ff0000
Global cursor.cursor
Global mode.s="default"
Global bg=CopyImage(wallpaper,#PB_Any)
ResizeImage(wallpaper,dw,dh);
ResizeImage(bg,rdw,rdh) ;
StartDrawing(ImageOutput(wallpaper))
DrawingMode(#PB_2DDrawing_AlphaBlend )
Box(0,0,dw,dh,RGBA(0,0,0,200))
StopDrawing()
cursor\blinkrate=500
cursor\ch=219
cursor\animrate=200
definecursor("color@ffff00:ff0000:00ff00:00ffff:0000ff:ff00ff")
definecursor("blinkrate@500")
definecursor("sym@176:177:178:219:178:177");124:124");
definecursor("animrate@50")
Global logpos=0
Global prompt=#True
SetWindowTitle(cli, "test")
;- main loop
Repeat
active=GetActiveWindow()
ev=WaitWindowEvent(0)
RefreshKeyboard()
If active = cli
If prompt=#True
a=scankeys()
If a
dokey(a)
EndIf
EndIf
EndIf
cli()
Until keydown(#AltKey_Esc) Or ev=#PB_Event_CloseWindow
Procedure cli()
StartDrawing(CanvasOutput(can2))
DrawingFont(FontID(fnt))
If IsImage(wallpaper)
DrawImage(ImageID(wallpaper),0,0)
Box(0,dh-fth,dw,fth,0)
Else
Box(0,0,dw,dh,0)
EndIf
shlen=((dw/ftw)-Len(prompt$))-1
shocli$=Mid(cli$,(cursor\pos+1)-shlen,shlen)
DrawText(0,dh-fth,prompt$+shocli$,fc)
;
DrawingMode(#PB_2DDrawing_XOr)
cursor()
If cursor\colors()>=0
DrawText((cursor\scpos+Len(prompt$))*ftw,dh-fth,Chr(cursor\ch),cursor\col)
EndIf
DrawingMode(#PB_2DDrawing_Default)
If ListSize(Logs())
SelectElement(Logs(),toplog)
For disp=0 To 40
disp=display(disp)
If disp>=(dh/fth)-1
toplog +1
EndIf
If NextElement(Logs())
; ne rien faire :)
Else
Break
EndIf
Next
EndIf
StopDrawing()
EndProcedure
Procedure display(disp)
DrawingMode(#PB_2DDrawing_Transparent)
col=RGB(255,255,255)
t$=Logs()
For c=1 To Len (t$)
DrawText(xp*ftw,disp*fth,Mid(t$,c,1),col)
xp+1
If xp>Round(dw/ftw,#PB_Round_Down)
xp=1
disp +1
EndIf
Next
ProcedureReturn disp
EndProcedure
Procedure scankeys()
For a=255 To 0 Step -1
If A<>16 And a<>161 And a<>160 And a<> 162 And a<>165 And a<>18 And a<>17 And a<>164
If Keydown(a)
If lastkey\id<> a
lastkey\id=a
lastkey\time=ElapsedMilliseconds()
lastkey\repeat=0
ProcedureReturn a
ElseIf ElapsedMilliseconds()-lastkey\time>350 Or lastkey\repeat>0
If ElapsedMilliseconds()-lastkey\time>50
lastkey\id=a
lastkey\time=ElapsedMilliseconds()
lastkey\repeat=1
ProcedureReturn a
EndIf
EndIf
ProcedureReturn 0
EndIf
EndIf
Next
lastkey\ID=-1
EndProcedure
Procedure dokey(a) ; check for keys
Select a
Case 37 ; <-
If cursor\pos>0
cursor\pos -1
EndIf
If cursor\pos<78 And cursor\scpos>0
cursor\scpos -1
EndIf
Case 39 ; ->
If cursor\pos<Len(cli$)
cursor\pos +1
EndIf
If cursor\scpos<78 And cursor\scpos<Len(cli$)
cursor\scpos+1
EndIf
; cursor\pos +1
Case 38 ; ^
Case 40 ; v
Case 8 ; backspace
If cli$<>""
cli$=Left(cli$,(cursor\pos)-1)+Right(cli$,Len(cli$)-cursor\pos)
cursor\pos -1
If cursor\pos<78 And cursor\scpos>0
cursor\scpos -1
EndIf
EndIf
Case 46;;supr
cli$=Left(cli$,(cursor\pos))+Right(cli$,(Len(cli$)-cursor\pos)-1)
Case 92,93,1,2,14 To 20,91,160,161,163 ;do nothing
Case 13
cursor\pos=0
cursor\scpos=0
Output(cli$)
cli$=""
Case 36 ; home
cursor\pos=0
cursor\scpos=0
Case 35 ; end
cursor\pos=Len(cli$)
If Len(cli$)>78
cursor\scpos=78
Else
cursor\scpos=Len(cli$)
EndIf
Default ; les autre touches
cli$=Left(cli$,cursor\pos)+Left(returnchar(a),1)+Right(cli$,Len(cli$)-cursor\pos)
cursor\pos+1
If cursor\pos<79
cursor\scpos+1
EndIf
EndSelect
EndProcedure
Procedure.i cursor()
If ListSize(cursor\colors())>0
If cursor\lastblink+cursor\blinkrate<=ElapsedMilliseconds()
cursor\lastblink=ElapsedMilliseconds()
If NextElement(cursor\colors())
Else
FirstElement(cursor\colors())
EndIf
cursor\col=cursor\colors()
EndIf
EndIf
If ListSize(cursor\char())>0
If cursor\lastframe+cursor\animrate<=ElapsedMilliseconds()
cursor\lastframe=ElapsedMilliseconds()
If ListSize(cursor\char())
If NextElement(cursor\char())
Else
FirstElement(cursor\char())
EndIf
cursor\ch=cursor\char()
EndIf
EndIf
EndIf
EndProcedure
Procedure RefreshKeyboard() ;rafraichis l'etat du clavier
CopyMemory(*buffer,*state,256) ; sauvegarde l'etat precedent pour comparaison (keyup)
GetKeyboardState_(*buffer)
EndProcedure
Procedure.s returnchar(key) ; renvoi le caractere saisi
r.s=Space(4)
ToAscii_(key,0,*buffer,@r,0)
ProcedureReturn r;Mid(r,1,1)
EndProcedure
Procedure Keydown(key) ; teste si une touche est enfoncée (continue)
If PeekB(*buffer+key)<0
ProcedureReturn 1
EndIf
EndProcedure
Procedure keyup(key) ; teste si une touche est lachée
If PeekB(*buffer+key)>=0 And PeekB(*state+key)<0
PokeB(*state+key,PeekB(*buffer+key))
ProcedureReturn 1
EndIf
EndProcedure
Procedure keypushed(key) ; teste si une touche est poussée (instant)
If PeekB(*buffer+key)<0 And PeekB(*state+key)>=0
PokeB(*state+key,PeekB(*buffer+key))
ProcedureReturn 1
EndIf
EndProcedure
Procedure keyon(key) ; teste si une touche est 'on' ver maj, num lock etc...
ProcedureReturn PeekB(*buffer+key) &%00000001;1
EndProcedure
Procedure.s getwallpaperpath()
String$ = Space(#MAX_PATH)
SystemParametersInfo_(#SPI_GETDESKWALLPAPER,#MAX_PATH,String$,0)
ProcedureReturn String$
EndProcedure
Procedure definecursor(cursor$)
dat.s=StringField(cursor$,2,"@")
com.s=StringField(cursor$,1,"@")
; definit le curseur a l'aide d'une string contenant l'atribut et les données de l'attribut
Select LCase(com)
Case "color" ; definit les couleurs en format rgb hexadecimal -1 pas de couleur ( pas d'affichage du curseur)
; free the color list du cursor
If ListSize(cursor\colors())
ClearList(cursor\colors())
EndIf
For col=1 To CountString(dat,":")+1
f$=StringField(dat,col,":")
AddElement(cursor\colors())
If f$="-1"
cursor\colors()=-1
Else
cursor\colors()=RGB(Val("$"+Mid(f$,1,2)),Val("$"+Mid(f$,3,2)),Val("$"+Mid(f$,5,2)))
EndIf
Next
Case "blinkrate"
cursor\blinkrate=Val(dat)
Case "sym" ; definit les symbols
If ListSize(cursor\char())
ClearList(cursor\char())
EndIf
For col=1 To CountString(dat,":")+1
f$=StringField(dat,col,":")
AddElement(cursor\char())
cursor\char()=Val(f$)
Next
Case "animrate"
cursor\animrate=Val(dat)
EndSelect
EndProcedure
Procedure Output(st$)
If ListSize(Logs())
LastElement(Logs())
EndIf
AddElement(Logs())
Logs()=st$
If ListSize(logs())>40
toplog +1
EndIf
EndProcedure