0

I was making a 2 players game in assembly 8086 and I encountered a problem in which if I press & hold both of the players movement buttons only one would move and the other will stop. I am using mov ah, 0bh int 21h for checking if there a keystroke after that if it detected a keystroke it will use mov ah, 0 int 16h and proceed to check the player key character (WS for one player arrows for the other)

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Demo
  • 15
  • 5
  • Use the [keyboard services](http://www.techhelpmanual.com/228-int_16h__keyboard_services.html) which permit you to detect key down and key up events separately. – fuz May 28 '22 at 10:18
  • @fuz but how does it help me get both keys? I need both of them to be detected as down keys at the same time... – Demo May 28 '22 at 10:20
  • Each key sends a “key down” event when it is pressed and a “key up” event when it is released. Track these events to learn which keys are pressed at which point in time. – fuz May 28 '22 at 10:21
  • @fuz I looked at the link you sent, but couldn't find the ah value needed for the "key down" event – Demo May 28 '22 at 10:23
  • Use INT 16h/AH=01h to check if a key is available. If yes, read it with INT 16h/AH=00h. Check the [scan code](http://www.techhelpmanual.com/57-keyboard_scan_codes.html) for whether it's a key-up or key-down event. – fuz May 28 '22 at 10:27
  • @fuz that was I was doing the whole time just instead of int16/ah=01h I use int 21h/ah = 0bh to not stop the program, but for some reason it only detects one of the keys, which is the problem I am having... – Demo May 28 '22 at 10:29
  • That's a DOS function and I think it mangles the input such that you can only see one key at a time. Not sure though. – fuz May 28 '22 at 10:31
  • @fuz ok... but that doesn't answer my original question xd – Demo May 28 '22 at 10:32
  • It does. Because the Int 16 interface and the Int 21 interface are two different things. Also, the Int 21 interface does not provide access to the scan code, which is what you need to detect key-up events. – fuz May 28 '22 at 10:34
  • Once again, I don't want key up events... I want key down events that will detect at the same time so it does not answer my question. – Demo May 28 '22 at 10:36
  • It does because if you grab them right at the source, no filtering occurs. If that still does not solve your problem, provide a [mcve] and I can try to find out what went wrong. However, without seeing your code it is impossible for me to tell what mistake you made. – fuz May 28 '22 at 10:37
  • Do you have a practical solution? (using the 21h events you mentioned)? – Demo May 28 '22 at 10:38
  • @fuz: int 16h does not provide key down and key up events, you'd have to monitor interrupt 09h calls for that. – ecm May 28 '22 at 10:44
  • Once again, you and I are not talking about the same things... you are giving out of the blue examples pal – Demo May 28 '22 at 10:46
  • @Demo Again, I don't think there is a solution using int 21h. I believe you have to use int 16h (but I might be wrong). What ecm said applies, too. – fuz May 28 '22 at 10:51
  • @fuz as I said, I am already using int 16h for reading the actual key... – Demo May 28 '22 at 10:53
  • @Demo Then why did you say “using the 21h events you mentioned?” – fuz May 28 '22 at 10:56
  • I misread your comment, I thought you meant that you think the solution to that is using 21h events... – Demo May 28 '22 at 10:56
  • Here's an example that hooks interrupt 09h to read the scancodes for key down and key up events: https://stackoverflow.com/questions/40961527/checking-if-a-key-is-down-in-ms-dos-c-c/40963633#40963633 – ecm May 28 '22 at 10:59
  • Hey @ecm this is a different language from the one I am using so that doesn't really help me xd – Demo May 28 '22 at 11:00
  • @Demo: It does depict all parts you need to have in order to do this yourself. If you don't know how to translate basic C code to assembly then you have a lot left to learn. – ecm May 28 '22 at 11:02
  • It is not about translating C code. I want & need to use basic assembly functions from int16h & int 21h. If you have a solution using that I would be very glad – Demo May 28 '22 at 11:03
  • You have to use (intercept) interrupt 09h and read the keyboard port to get key down and key up events for scancodes, which you can use in your logic to determine what keys are pressed at any given time. You can do that in assembly, too. The higher-level interrupt 16h and interrupt 21h services **do not** provide any possibility to detect that keys are pressed at the same time. – ecm May 28 '22 at 11:06
  • @ecm do you mean ah = 09h or int 09h? – Demo May 28 '22 at 11:18
  • Not "ah = 09h". The line `setvect(0x09, keyb_int);` in the example I linked means to override the ISR for *interrupt 09h (IRQ #1 handler)*, nothing to do with interrupts 16h or 21h. As I said the interrupt 16h and interrupt 21h services (with any function number in ah) do not provide any way to detect simultaneous keypresses. – ecm May 28 '22 at 11:22
  • 2
    You may wish to see this answer https://stackoverflow.com/a/37033680/3857942 . I'm tempted to mark this as a duplicate. – Michael Petch May 28 '22 at 13:21

0 Answers0