英文:
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 './stores/store';
window.addEventListener('click', 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="checkbox" 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 <script>
tag of my Svelte component:
import { is_shift_pressed } from './stores/store';
window.addEventListener('click', 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="checkbox" 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.
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("Clicked", evt.shiftKey);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论