# Adding EEG Triggers to keyboard responses

**URL:** <https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154>\
**Category:** Builder\
**Created:** [November 25, 2022, 8:37am UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154 "2022-11-25T08:37:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![csy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/csy/32/36237_2.png) [@csy](https://discourse.psychopy.org/u/csy)\
**Post date:** [November 25, 2022, 8:37am UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154/1 "2022-11-25T08:37:16Z")

</div>

If this template helps then use it. If not then just delete and start from scratch.

**OS** (e.g. Win10): Win10  
**PsychoPy version** (e.g. 1.84.x): 2020.1.3  
**Standard Standalone? (y/n)** If not then what?:  
**What are you trying to achieve?:**  
I’m trying to add EEG triggers/markers (via cpod) whenever I have a keyboard press (either button ‘1’ or ‘2’)

**What did you try to make it work?:**  
I’ve tested the eeg trigger code separately and it works, but when i try to insert a code component to specify for the EEG trigger whenever a keyboard button is pressed, it doesn’t work. keyResponse is the name of my keyboard press. Code component as follow:

if keyResponse.keys == ‘1’:  
for bm in range(1):  
mask = 2 \*\* bm  
print("activate\_line bitmask: ", mask)  
dev.activate\_line(bitmask=mask)  
elif keyResponse.keys == ‘2’:  
for bm in range(1):  
mask = 2 \*\* bm  
print("activate\_line bitmask: ", mask)  
dev.activate\_line(bitmask=mask)

**What specifically went wrong when you tried that?:**  
The paradigm runs but i dont get the EEG trigger on my EEG recording software. I am however able to get a trigger if i just run the following:

```
for bm in range(1):
    mask = 2 ** bm
    print("activate_line bitmask: ", mask)
    dev.activate_line(bitmask=mask)

```

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [November 25, 2022, 11:51pm UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154/2 "2022-11-25T23:51:06Z")

</div>

Hi There,

Is there a reason to communicate with your device via code component rather than the inbuild parallel/serial port components? (Details [here](https://psychopy.org/hardware/index.html))

If your keyboard response is ending the routine you can then have a second routine with a parallel/serial port at the start?

Becca

---

<div class="post-metadata">

**Author:** ![csy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/csy/32/36237_2.png) [@csy](https://discourse.psychopy.org/u/csy)\
**Post date:** [November 28, 2022, 2:39am UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154/3 "2022-11-28T02:39:17Z")

</div>

Hi Becca,

Thanks for the reply!

I didnt use the inbuild parallel port as we are using a cpod, not sure if it is compatible with the parallel/serial port components?

The keyboard response isn’t ending the routine, it is a loop that repeats where different stimuli will be shown one after another and keyboard response is required for each stimuli.

---

<div class="post-metadata">

**Author:** ![csy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/csy/32/36237_2.png) [@csy](https://discourse.psychopy.org/u/csy)\
**Post date:** [November 29, 2022, 8:27am UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154/4 "2022-11-29T08:27:11Z")

</div>

Have tried using the inbuild parallel port but it doesn’t seem to work with cpod and doesn’t seem to have a trigger/marker produced.

---

<div class="post-metadata">

**Author:** ![Becca](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/becca/32/34260_2.png) [@Becca](https://discourse.psychopy.org/u/Becca)\
**Post date:** [December 9, 2022, 8:18pm UTC](https://discourse.psychopy.org/t/adding-eeg-triggers-to-keyboard-responses/32154/5 "2022-12-09T20:18:14Z")

</div>

I would try adding the code component then where you have something like this in the begin routine tab:

```auto
key_count = 0

```

then in the each frame tab

```auto
if len(key_resp.keys)>key_count:
    # send your trigger here
    key_count += 1

```

Where I’ve marked “send your trigger” use the usual code snippet you have been using to interact with your device if that is working!

Becca
