The FreeBasic Wiki says ON KEY is not supported because it is "fake event driven programming." I never knew it was fake. I guess that means it always did a busy loop behind the scenes. Did it? It also says it could be emulated by an external library. How?
I changed my code to use INKEY but that just gives me a busy loop. I would rather not peg the CPU constantly.
So how does one do event driven programming in FreeBasic (that is QB compatible) without peaking the CPU with a busy loop? (Yes, I want QB compatible. The is the whole point of this language.)
Thanks...
By the way are you guys all unemployed or something? I never thought FreeBasic would be completed. Then I come back and find this. The FBIde looks good too. Didn't the author of that quit working on it?
dicen
ON KEY
Um, i'm not sure you can do a QB compatible keyboard event system. I'd suggest your best bet is to do a loop checking for keys, but if your worried about CPU usage, put a Sleep 10 in it. That'll make it sleep for roughly 10 milliseconds, which will release the timeslice so your program won't hog the CPU, and it's a little too fast for the average person to type, that'd be like 100 letters a second
Hello,
You may consider using threads, as that is a solution more suited for a multitasking environment, unless of course you're running FB on DOS, in which case I'll say "..."
btw, as much as some of us love QB, you're quite wrong if you think "the whole point of the language" is to be compatible with QB. C libraries anyone? Intuitive pointer syntax? OOP? The list goes on.
You may consider using threads, as that is a solution more suited for a multitasking environment, unless of course you're running FB on DOS, in which case I'll say "..."
btw, as much as some of us love QB, you're quite wrong if you think "the whole point of the language" is to be compatible with QB. C libraries anyone? Intuitive pointer syntax? OOP? The list goes on.
Sleep 10 works but a native ON KEY would be better. Threads are not in QB. If I wanted to use C libraries, OOP, etc... I would chose the many other already existing languages. Visual Basic, C#, Visual C++, Java, gcc, g++, etc... The ground floor for this language is QB. It grows from there.cha0s wrote:Hello,
You may consider using threads, as that is a solution more suited for a multitasking environment, unless of course you're running FB on DOS, in which case I'll say "..."
btw, as much as some of us love QB, you're quite wrong if you think "the whole point of the language" is to be compatible with QB. C libraries anyone? Intuitive pointer syntax? OOP? The list goes on.
dicen
I'm not picking on Vendan. It's relatively simple to effect "On Key" in a Windoze-based fbgfx prog.Vendan wrote:Um, i'm not sure you can do a QB compatible keyboard event system. I'd suggest your best bet is to do a loop checking for keys, but if your worried about CPU usage, put a Sleep 10 in it. That'll make it sleep for roughly 10 milliseconds, which will release the timeslice so your program won't hog the CPU, and it's a little too fast for the average person to type, that'd be like 100 letters a second
Code: Select all
'On_Key in fb GFX (not console), Windows only
' requires fb v17b >= Jan 2007
#include once "windows.bi"
#include once "crt.bi"
#include once "fbgfx.bi"
using fb
'
declare function On_Key(idHook as integer,_
lpfn as HOOKPROC) as integer
'
declare function KBProc1(ByVal Code As integer, _
ByVal wParam As integer,_
ByVal lParam As integer) As integer
'
dim shared KBHandle as HHOOK
dim shared hwnd as HANDLE
dim as integer res
dim shared as integer idKey,LastKey
'
type _hookstruct_ 'see MDSN "KeyboardProc"
as ubyte state : 1
as ubyte prevkey : 1
as ubyte alt : 1
as ubyte reserved : 4
as ubyte extended : 1
scancode as ushort
repeat as uinteger
end type
'
screen 11 'requires GFX screen, not just console
ScreenControl GET_WINDOW_HANDLE,hwnd
'
On_Key(37,@KBProc1) 'see MultiKey scancodes, winuser.bi
'37 = "k", 59 = "F1"
'
while inkey<>chr(27)' "Esc" to end
print time
sleep 15000
wend
'
UnhookWindowsHookEx(KBHandle)
print
print "Sleeping to Exit.. press a key"
sleep
end
'
function On_Key(idHook as integer,_
lpfn as HOOKPROC) as integer
idKey=idHook
KBHandle=SetWindowsHookEx(WH_KEYBOARD,_
lpfn,_
NULL,_
GetWindowThreadProcessId(hwnd,NULL))
End function
'
function KBProc1(ByVal Code As integer, _
ByVal wParam As integer,_
ByVal lParam As integer) As integer
'
dim Hook as _hookstruct_
dim as integer scode,altkey,ctrlkey,shiftkey
'
if (Code = HC_ACTION) then
memcpy(@Hook,@lParam,len(Hook))
scode=Hook.scancode
'
altkey=(lParam AND (1 shl 29))
ctrlkey=((GetKeyState(VK_CONTROL) AND (1 shl 15)) <> 0)
shiftkey=GetKeyState(VK_SHIFT) AND (1 shl 15)
'
if scode=idKey and shiftkey>0 then 'don't move this!!!
print " Shift + My Key pressed"
return 1 'this will block the key
end if
'
if scode=idKey and ctrlkey<> -1 then 'leave after SHIFT!
print " My bare Key pressed"
return 1 'this will block the key
end if
'
if scode-8192 = idKey and altkey>0 then
print " Alt + My Key pressed"
return 1 'this will block the key
end if
'
if scode = idKey and ctrlkey= -1 then
print " Ctrl + My Key pressed"
return 1 'this will block the key
end if
'
if wParam=91 then return 1 'trap LeftWin key
'
end if
'
'print scode
print "Keycode: ";wParam
if wparam=19 then beep 'Pause/Break key
'
'pass-on keys we don't want
KBProc1=CallNextHookEx(KBHandle,_
Code, wParam, lParam)
End Function