Typescript store updating one click late in a click event listener.

huangapple go评论57阅读模式
英文:

Typescript store updating one click late in a click event listener

问题

I'm trying to know if the shift key is being pressed when clicking a checkbox (I'm using Svelte). I need a store that will be updated in "real time" according to the state of the shift key.

我正在尝试确定在单击复选框时是否按下了Shift键(我正在使用Svelte)。我需要一个存储库,根据Shift键的状态实时更新。

I have a property in a store.ts file:

我在store.ts文件中有一个属性:

export let is_shift_pressed = writable(false)

In the <script> tag of my Svelte component:

在我的Svelte组件的<script>标签中:

import { is_shift_pressed } from &#39;./stores/store&#39;;

window.addEventListener(&#39;click&#39;, async function (e) {
   // e.shiftKey has always the correct value that I expect
   is_shift_pressed.set(e.shiftKey);
   // but the store is late of 1 click
});

I have a checkbox:

我有一个复选框:

<input type=&quot;checkbox&quot; on:click={checkboxClicked}/>

When the checkbox is clicked, I try to display the value of the store. It should be equal to the value of e.shiftKey at the moment when the store was updated.

当单击复选框时,我尝试显示存储库的值。它应该等于存储库更新时的e.shiftKey的值。

function checkboxClicked(){
   console.log(shiftPressed)
   // prints the value of the previous click
}
英文:

I'm trying to know if the shift key is being pressed when clicking a checkbox (I'm using Svelte). I need a store that will be updated in "real time" according to the state of the shift key.

I have a property in a store.ts file:

export let is_shift_pressed = writable(false)

In the &lt;script&gt; tag of my Svelte component:

import { is_shift_pressed } from &#39;./stores/store&#39;;

window.addEventListener(&#39;click&#39;, async function (e) {
   // e.shiftKey has always the correct value that I expect
   is_shift_pressed.set(e.shiftKey);
   // but the store is late of 1 click
});

I have a checkbox:

&lt;input type=&quot;checkbox&quot; on:click={checkboxClicked}/&gt;

When the checkbox is clicked, I try to display the value of the store. It should be equal to the value of e.shiftKey at the moment when the store was updated.

function checkboxClicked(){
   console.log(shiftPressed)
   // prints the value of the previous click
}

答案1

得分: 0

只要查找传递给您的点击处理程序的event中的Shift键:

	function checkboxClicked(evt){
		 console.log("Clicked", evt.shiftKey);
	}
英文:

Just look for the shift key on the event that is passed to your click handler:

<!-- language: lang-js -->

function checkboxClicked(evt){
	 console.log(&quot;Clicked&quot;, evt.shiftKey);
}

huangapple
  • 本文由 发表于 2023年5月10日 21:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定