send keys?

Just starting out? Need help? Post your questions and find answers here.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

send keys?

Post by doodlemunch »

i tried some send key examples but they dont work nice i mean i want to simulate real key presses i can do this with the mouse for example but my idea is to use the keyboard instead of the mouse for my proggy i dont know if visual basics sendkey does this or not but it should i want to simulate not just send a key to a program
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Try this

Code: Select all

Procedure SendKey(Option.l) 
  ; Author : Oliv
  keybd_event_(Option,0,0,0) ; Simulate Keypress
  keybd_event_(Option,0,#KEYEVENTF_KEYUP,0) ; Simulate release key 
  Delay(60) 
EndProcedure


Delay(5000)
SendKey(#VK_F5)     ; Simulate F5 Keystroke
SendKey(#VK_RETURN) ; Simulate Enter Keystroke
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

no it doesnt simulate really a keystroke like if it was passed from the hardware ;(;(;(;(
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: send keys?

Post by PB »

Read this: viewtopic.php?t=4876
And then this: viewtopic.php?t=3766
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
doodlemunch
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Apr 05, 2005 11:20 pm

Post by doodlemunch »

thanks :D i read it and compiled all but none simulates a real keystroke
theres many proggys that can tell you when a keys been pressed ((not a keylogger!!!)) but with no snippet it detected a key being pressed looks like its not really simulatingg the keystroke PBs aproach seems the best but it doesnt simulate a real keystroke for what i could see

i thought it was possibel to really really simulate a keystroke i just think im '' not up to the challenge ''
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> PBs aproach seems the best but it doesnt simulate a real keystroke for
> what i could see

My tip/procedure uses the "keybd_event" API which is the native way that
Windows sends keystrokes to the system... so I don't know why you can't
detect them. I use my SendKeys procedure every day, at home and work,
to "type" text into other apps all the time... with no problems whatsoever.

If you want proof, here's an example that simulates keypresses and also
does keylogging of them as they're typed... you can't argue with that! ;)

Code: Select all

If OpenWindow(0,200,200,400,100,#PB_Window_SystemMenu,"Typing Test",0)
  If CreateGadgetList(WindowID())
    TextGadget(0,0,0,400,20,"Click the button to simulate keystrokes!")
    TextGadget(1,0,30,400,20,"Simulated and detected: ")
    ButtonGadget(2,0,60,100,25,"Start typing")
    Repeat
      ev=WaitWindowEvent()
      If ev=#PB_Event_Gadget And EventGadgetID()=2
        SetGadgetText(1,"Simulated and detected: ")
        For a=65 To 90
          ; Next line simulates the keypresses.
          keybd_event_(a,0,0,0) : keybd_event_(a,0,#KEYEVENTF_KEYUP,0)
          If GetAsyncKeyState_(a)<>0 ; This line detects them (keylogger).
            a$=GetGadgetText(1)+Chr(a)
            SetGadgetText(1,a$)
          EndIf
        Next
      EndIf
    Until ev=#PB_Event_CloseWindow
  EndIf
EndIf
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
marcos.exe
User
User
Posts: 20
Joined: Fri Jan 17, 2020 8:20 pm

Re: send keys?

Post by marcos.exe »

Hi!
I know the post is old, but I would like to know if there is already some way to have a keystroke simulation that actually works well.
All the examples I found on the forum do not work in certain applications, for example, in games running in full screen, or even many in window.
That's exactly what I needed an effective code for.

Original.

Oi!
Eu sei que o post é antigo, mas eu gostaria de saber se já existe alguma forma de ter uma simulação de pressionamento de teclas que realmente funcione bem.
Todos os exemplos que encontrei no forum não funciona em determinados aplicativos, como por exemplo, em jogos executados em tela cheia, ou até mesmo muitos em janela.
Era justamente para isso que precisava de um código eficaz.
When our generation/OS updates, we either update ourselves, or we are removed.
But we are never fully uninstalled.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: send keys?

Post by BarryG »

Hi, which games doesn't it work with? I'm able to use keybd_event_() with a few full-screen games that I just tried. Or which windowed app doesn't it work with?
AZJIO
Addict
Addict
Posts: 1372
Joined: Sun May 14, 2017 1:48 am

Re: send keys?

Post by AZJIO »

marcos.exe
User
User
Posts: 20
Joined: Fri Jan 17, 2020 8:20 pm

Re: send keys?

Post by marcos.exe »

Does not work with mame emulator.
SendInput doesn't either.
I'm trying PureAutoIt, but not even the example scripts that come with the package are working in this version of PureBasic.
When our generation/OS updates, we either update ourselves, or we are removed.
But we are never fully uninstalled.
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: send keys?

Post by BarryG »

marcos.exe wrote: Sat May 07, 2022 7:15 pmDoes not work with mame emulator
MAME is a special case that ignores them -> http://forum.arcadecontrols.com/index.p ... 95616.html
Last edited by BarryG on Tue May 10, 2022 2:32 am, edited 1 time in total.
marcos.exe
User
User
Posts: 20
Joined: Fri Jan 17, 2020 8:20 pm

Re: send keys?

Post by marcos.exe »

Well, according to what I could find about MAME, is that those who know how to do it, do not that, due to malicious users.
Programmers are encouraged to send joystick buttons. I have no idea how to do this.

I used an old MAME emulator that runs on the command line (v.0.79), and the keystroke works fine.
I noticed that the most current versions of MAME, or at least the MAME32 ones, didn't work.
I tested it with some less current ones like 0.100 which was the lowest, and it doesn't work.

It was then that I discovered that the developers made it clear that no future version of MAME will be able to allow this feature, as a security measure, to prevent illegal cabinet owners from creating something prohibited. I didn't quite understand. But...

They developed a plugin to do what I intended, but I haven't tested it yet.
https://docs.mamedev.org/plugins/inputmacro.html

As for the scripts I made, they work on the version I mentioned at the beginning, and on gens, which is a Genesis/MegaDrive emulator.
I haven't tested it with other emulators, but I already know that if it doesn't work, it could be for the same reason as the MAME developers.

OK. It must be done, what needs to be done.

Thank you all for your help.
When our generation/OS updates, we either update ourselves, or we are removed.
But we are never fully uninstalled.
Post Reply