Search code examples
smalltalkkeydownvisualworks

Simulate keydown event


How can I simulate a key (ctrl) being hold down while some other code is executed? What would be the implementation of the following method?

self ctrlDownWhile: [self doSomething]

Solution

  • You could try to "trick" the input state, by changing its "ctrlDown" state. The bad news is that it does not have a setter-method to access it (maybe only in my version), so you may have to get there with a trick:

    ctrlDownWhile:aBlock
      "aBlock will see ctrlDown as true"
    
      |indexOfCtrlState|
    
      indexOfCtrlState := InputState allInstVarNames indexOf:'ctrlState'.
    
      InputState default instVarAt:indexOfCtrlState put:1.   
      aBlock  
        ensure: [
          InputState default instVarAt:indexOfCtrlState put:0.
        ].
    

    an alternative is to create keyPress & keyRelease-events for the CTRL-key, and enqueue them into the WindowSensor, before and after the block's evaluation.