Ooops
November 30, 2020, 02:34:00 AM

Author Topic: [bb] Small keyboard handler by Vector Viper [ 1+ years ago ]  (Read 533 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Small keyboard handler
Author : Vector Viper
Posted : 1+ years ago

Description : Allows easy config of keys+very flexible key reading

Code :
Code: BlitzBasic
  1. ;This is a little keyboard handler-I hope it is useful to someone...
  2. ;By Aaron Howald (aka Vector Viper)
  3. ;It can time key presses, releases, and holds.
  4. ;It also allows for easier keyboard configuration.
  5.  
  6. ;please report back how well this works-my keyboard refuses
  7. ;to register more than 3 keys pressed at the same time,
  8. ;and no more than 5 pressed and held down at once
  9. ;also, holding down Lctrl,alt,space,altgr,Rctrl makes the left
  10. ;arrow key not register, but the other arrows still do!
  11. ;I was planning some neat weapons select by holding keys, but now...hmmm...
  12. ;(update)
  13. ;I since got a different keyboard-it CAN register any# of keys...
  14.  
  15. ;uses these keycodes here
  16. ;u=200, d=208, l=203, r=205,space=57,l-alt=56 ,l ctrl=29,altgr=184,rctrl=157
  17.  
  18. Const screenwidth=640
  19. Const screenheight=480
  20. Const screendepth=16
  21. Const displaymode=0;0=FS, 2=windowed
  22. Graphics screenwidth,screenheight,screendepth,displaymode
  23.  
  24. Dim keytranslate(10)
  25. ;each slot is hard-coded function, to the program.
  26. ;the# returned is the key# attached to the function...
  27. ;                0      1   2   3    4      5     6     7     8     9      10                
  28. ;tempxvalues;QUIT/MENU UP DOWN LEFT RIGHT FIRE1 FIRE2 FIRE3 FIRE4 SHIELD    ?
  29. ;               ESC    (same            ) space l-alt l-ctrl altgr rctrl
  30. ;esc=1, u=200, d=208, l=203, r=205,        57     56   29    184    157
  31. Restore keyboard_load
  32. .keyboard_load
  33. Data 1,200,208,203,205,57,56,29,184,157
  34. For x=0 To 9
  35.  Read temp
  36.  keytranslate(x)=temp
  37. Next
  38.  
  39. Dim keyread(10);status of key assiged to the function
  40.  
  41. While KeyDown(1)=0
  42.  
  43.         Flip
  44.        
  45.         Cls
  46.  
  47. ; put this in at the top of the main loop
  48.       For x=0 To 10 ;scan keytranslate
  49.         y=KeyDown(keytranslate(x)) ;returns 0 or 1
  50.  
  51.         Select y+1
  52.         Case 1;key is NOT pressed NOW
  53.         If keyread(x)<0 Then keyread(x)=0;second cycle, set to not pressed
  54.         If keyread(x)>0 Then keyread(x)=-1;JUST released, 1st cycle        
  55.         ;can have more "cycles" if desired...can count from -10 to 0 say...
  56.  
  57.         ;can time releases too-just put these in place of the 2 above:
  58.         ;If keyread(x)>0 Then keyread(x)=0
  59.         ;keyread(x)=keyread(x)-1
  60.         ;0 would then be the "key has changed" flag
  61.  
  62.         Case 2 ;key pressed NOW
  63.         If keyread(x)<0 Then keyread(x)=0
  64.         keyread(x)=keyread(x)+1 ;times out keypress as long as held down
  65.         ;actual time varies of course-sync to vblank?
  66.         End Select
  67.       Next
  68.  
  69. ;General keyread values:
  70. ; 0=not pressed, 1=just pressed, 2 or more=held, -1=just released
  71.  
  72. ;examples:
  73. ;If keyread(5)=-1;fire when key just released (weapons charge, or fighting game move)
  74.  
  75. ;If keyread(5)>1;key is pressed,returns every cycle
  76. ;if keyread(5)=1;pressed key-returns only once!
  77. ;If keyread(5)>50 ;returns "pressed" continously after a delay
  78. ;if keyread(5)=50 ;returns "pressed" once after a delay
  79. ;if keyread(5)=1 or keyread(5)>30 ;returns once, then delay to repeating...
  80.  
  81. ;I think this works very well!
  82.  
  83. ;do rest of program, blah....
  84.  
  85. ;show the values
  86. For x=0 To 10
  87. Text x*30,100,keyread(x)
  88. Next
  89.  
  90.  
  91. Wend
  92. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal