Voici un code pour tester le clavier quand les fonctions standard de PB KeyboardReleased et KeyboardPressed ne fonctionnent pas bien avec openscreen (sur certaines configs)
Code : Tout sélectionner
;*****************************************************************************************
;*
;* myKeyboardKeys
;*
;* Windows callback routine to get keyboard state with openscreen when
;* standard pb KeyboardReleased and KeyboardPressed don't work
;*
;* djes (djes@free.fr)
;* http://www.bgames.org
;*
;* 09/30/2007 : first version
;*
;*****************************************************************************************
Global Dim myKeyboardKeys.l(255)
Global *myKeyboard_pb_callback
;*****************************************************************************************
Procedure myKeyboardCB(WindowID.l, Message.l, wParam.l, lParam.l)
Select Message
; Case #WM_CHAR
; MessageRequester("#WM_CHAR","Character typed : Code "+ Str(wParam)+"="+Chr(wParam)+" ; Code 2 :"+Hex(lParam))
Case #WM_KEYDOWN
; wParam
; Specifies the virtual-key code of the nonsystem key.
; lParam
; Specifies the Repeat count, scan code, extended-key flag, context code, previous key-state flag, And transition-state flag, As shown in the following table.
; 0-15
; Specifies the Repeat count For the current message. The value is the number of times the keystroke is autorepeated As a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the Repeat count is Not cumulative.
; 16-23
; Specifies the scan code. The value depends on the OEM.
; 24
; Specifies whether the key is an extended key, such As the right-hand ALT And CTRL keys that appear on an enhanced 101- Or 102-key keyboard. The value is 1 If it is an extended key; otherwise, it is 0.
; 25-28
; Reserved; do not use.
; 29
; Specifies the context code. The value is always 0 For a WM_KEYDOWN message.
; 30
; Specifies the previous key state. The value is 1 If the key is down before the message is sent, Or it is zero If the key is up.
; 31
; Specifies the transition state. The value is always zero For a WM_KEYDOWN message.
; MessageRequester("#WM_KEYDOWN","Character typed : Code "+ Str(wParam)+"="+Chr(wParam)+" ; Code 2 :"+Hex(lParam))
myKeyboardKeys(wParam)=1
Case #WM_KEYUP
; wParam
; Specifies the virtual-key code of the nonsystem key.
; lParam
; Specifies the Repeat count, scan code, extended-key flag, context code, previous key-state flag, And transition-state flag, As shown in the following table.
; 0-15
; Specifies the Repeat count For the current message. The value is the number of times the keystroke is autorepeated As a result of the user holding down the key. The Repeat count is always one For a WM_KEYUP message.
; 16-23
; Specifies the scan code. The value depends on the OEM.
; 24
; Specifies whether the key is an extended key, such As the right-hand ALT And CTRL keys that appear on an enhanced 101- Or 102-key keyboard. The value is 1 If it is an extended key; otherwise, it is 0.
; 25-28
; Reserved; do not use.
; 29
; Specifies the context code. The value is always 0 For a WM_KEYUP message.
; 30
; Specifies the previous key state. The value is always 1 For a WM_KEYUP message.
; 31
; Specifies the transition state. The value is always 1 For a WM_KEYUP message.
; MessageRequester("#WM_KEYUP","Character typed : Code "+ Str(wParam)+"="+Chr(wParam)+" ; Code 2 :"+Hex(lParam))
myKeyboardKeys(wParam)=2
Default
EndSelect
CallWindowProc_(*myKeyboard_pb_callback, WindowID.l, Message.l, wParam.l, lParam.l)
EndProcedure
;*****************************************************************************************
Procedure myKeyboardReleased(code.l)
If mykeyboardkeys(code)=2
mykeyboardkeys(code)=0
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
;*****************************************************************************************
Procedure myKeyboardPressed(code.l)
If mykeyboardkeys(code)=1
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
;*****************************************************************************************
;* Need to be called after openscreen
Procedure myKeyboardInit()
If ScreenID()=0
MessageRequester("myKeyboardInit","Screen not opened")
End
EndIf
;Our callback working with openscreen
*myKeyboard_pb_callback=GetWindowLong_(ScreenID(), #GWL_WNDPROC)
If SetWindowLong_(ScreenID(), #GWL_WNDPROC, @myKeyboardCB())=0
MessageRequester("myKeyboardInit","Can't intercept keys")
End
EndIf
EndProcedure
;*****************************************************************************************
InitSprite()
;OpenWindow(0, 0, 0, 220, 160, "A screen in a window...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;OpenWindowedScreen(WindowID(0), 0, 0, 160, 160, 0, 0, 0)
;Usual PB callback for the Windows OS allowing you to process window events
;SetWindowCallback_( @WindowCB())
ExamineDesktops()
OpenScreen(DesktopWidth(0),DesktopHeight(0),DesktopDepth(0),"")
myKeyboardInit()
CreateSprite(1,100,100)
StartDrawing(SpriteOutput(1))
Box( 0, 0,100,100,RGB(250,20,20))
Box(10,10, 80, 80,RGB(50,20,220))
Circle(50,50,30,RGB(200,200,50))
StopDrawing()
x=DesktopWidth(0)/2-SpriteWidth(1)/2
y=DesktopHeight(0)/2-SpriteHeight(1)/2
;*****************************************************************************************
Repeat
;Repeat
; Event = WindowEvent()
; If Event = #PB_Event_CloseWindow
; End
; EndIf
;Until Event = 0
If myKeyboardPressed(#PB_Shortcut_Up)
y-1
EndIf
If myKeyboardReleased(#PB_Shortcut_Down)
y+1
EndIf
ClearScreen(0)
DisplaySprite(1,x,y)
StartDrawing(ScreenOutput())
text.s="myKeyboardKeys test"
DrawText(DesktopWidth(0)/2-TextWidth(text)/2,50,text,RGB($FF,$FF,$FF),0)
text.s="Up Arrow to move up, press and release Down Arrow to move down"
DrawText(DesktopWidth(0)/2-TextWidth(text)/2,DesktopHeight(0)/2-TextHeight(text)/2,text,RGB($FF,$FF,$FF),0)
StopDrawing()
FlipBuffers()
Until myKeyboardReleased(#PB_Shortcut_Escape)
End