Skip to content

Commit 1b68274

Browse files
committed
Allow checking if button is still pressed
1 parent fc43f71 commit 1b68274

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

Sources/GateEngine/System/HID/GamePad/GamePad.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ extension GamePad {
405405
}
406406
}
407407

408-
// Returns a receipt for the current press or nil if not pressed
408+
/// Returns true if newly pressed for given `receipt`. If button is held down this returns true only the first time and then false until newly pressed again
409409
public func isPressed(ifDifferent receipt: inout InputReceipts) -> Bool {
410410
guard self.isPressed else { return false }
411411
let key = ObjectIdentifier(self)
@@ -415,6 +415,17 @@ extension GamePad {
415415
receipt.values[key] = currentReceipt
416416
return true
417417
}
418+
419+
/// Returns true if the button was already pressed for given `receipt` using the `isPressed(ifDifferent:_)` varient. If button is still held down this returns true
420+
public func isPressed(ifUnchanged receipt: inout InputReceipts) -> Bool {
421+
guard self.isPressed else { return false }
422+
let key = ObjectIdentifier(self)
423+
if let receipt = receipt.values[key], receipt == currentReceipt {
424+
return true
425+
}
426+
receipt.values[key] = currentReceipt
427+
return false
428+
}
418429

419430
/**
420431

Sources/GateEngine/System/HID/Keyboard/Keyboard.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,75 @@ extension Keyboard {
317317
}
318318
return false
319319
}
320+
321+
/**
322+
Returns a receipt for the current press or nil if not pressed.
323+
- parameter receipt: An existing receipt from a previous call to compare to the current pressed state.
324+
- parameter modifiers: Key modifiers required for a press to be considered valid.
325+
- returns: A receipt if the key is currently pressed and the was released since the provided receipt.
326+
- note: This function does **not** store `block` for later execution. If the function fails the block is discarded.
327+
*/
328+
public func isPressed(
329+
ifUnchanged receipt: inout InputReceipts,
330+
andUsing modifiers: KeyboardModifierMask = []
331+
) -> Bool {
332+
guard isPressed, keyboard.modifiers.contains(modifiers) else { return false }
333+
let key = ObjectIdentifier(self)
334+
if isKeyVirtual == false {
335+
if let receipt = receipt.values[key], receipt == currentReceipt {
336+
return true
337+
}
338+
receipt.values[key] = currentReceipt
339+
return false
340+
}
341+
switch self.key {
342+
case .shift(.anyVariation):
343+
if keyboard.button(.shift(.leftSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
344+
return true
345+
}
346+
if keyboard.button(.shift(.rightSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
347+
return true
348+
}
349+
case .alt(.anyVariation):
350+
if keyboard.button(.alt(.leftSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
351+
return true
352+
}
353+
if keyboard.button(.alt(.rightSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
354+
return true
355+
}
356+
case .host(.anyVariation):
357+
if keyboard.button(.host(.leftSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
358+
return true
359+
}
360+
if keyboard.button(.host(.rightSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
361+
return true
362+
}
363+
case .control(.anyVariation):
364+
if keyboard.button(.control(.leftSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
365+
return true
366+
}
367+
if keyboard.button(.control(.rightSide)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
368+
return true
369+
}
370+
case .enter(.anyVariation):
371+
if keyboard.button(.enter(.standard)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
372+
return true
373+
}
374+
if keyboard.button(.enter(.numberPad)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
375+
return true
376+
}
377+
case .character(let character, .anyVariation):
378+
if keyboard.button(.character(character, .standard)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
379+
return true
380+
}
381+
if keyboard.button(.character(character, .numberPad)).isPressed(ifUnchanged: &receipt, andUsing: modifiers) {
382+
return true
383+
}
384+
default:
385+
break
386+
}
387+
return false
388+
}
320389

321390
/**
322391
Returns a receipt for the current press or nil if not pressed.

0 commit comments

Comments
 (0)